
#include "config.h"
#include "user_prg.h"
#include "main.h"


void prg_init_val(void)   //先初始化，再从flash中读出用户数据分别存入ram1和ram2
{
	uint16_t	u16_idx	= 0u;

	for(u16_idx = 0u; u16_idx < CH_NUM_DI; u16_idx ++)//初始化滤波数据
	{
		g_st_ram1.run_auxiliary_data.sta_di_filt[u16_idx].u16_filt_pram	  = DI_FILT_DFT;
		g_st_ram1.run_auxiliary_data.sta_di_filt[u16_idx].b8_thld_sta  	  = BFALSE;
		g_st_ram1.run_auxiliary_data.sta_di_filt[u16_idx].b8_vld_sta      = BFALSE;
		/* backup global variable */
		g_st_ram2.run_auxiliary_data.sta_di_filt[u16_idx].u16_filt_pram	  = DI_FILT_DFT;
		g_st_ram2.run_auxiliary_data.sta_di_filt[u16_idx].b8_thld_sta     = BFALSE;
		g_st_ram2.run_auxiliary_data.sta_di_filt[u16_idx].b8_vld_sta      = BFALSE;
	}

	for(u16_idx = 0u; u16_idx < NUM_LOGIC; u16_idx ++) //初始化逻辑快运行值
	{
		g_st_ram1.run_display_data.layer_control_value[u16_idx]	= BFALSE;		
		g_st_ram2.run_display_data.layer_control_value[u16_idx]	= BFALSE;
	
	}
	b8_uart_txflag=BTRUE;  //置发送空闲
	g_st_ram1.run_display_data.u16_vrefint_cal=*VREFINT_CAL_ADDR;  //基准校准地址
	g_st_ram2.run_display_data.u16_vrefint_cal=*VREFINT_CAL_ADDR;
	g_st_ram1.run_display_data.u16_ts_cal1=*TEMP30_CAL_ADDR;      //温度校准
	g_st_ram2.run_display_data.u16_ts_cal1=*TEMP30_CAL_ADDR;
	prg_read_flash();//读取flash中的用户数据存入ram1和ram2
	
	
}

static void prg_handle_input(const uint16_t u16a_dat[], uint8_t u8_mcu_addr)
{
	uint16_t	u16_di_dat	= 0u;
	uint16_t	u16_idx		= 0u;
	bool_t		b8_curt_sta	= BFALSE;
	uint8_t		u8_ch_fault		= 0u;
	
	
	uint16_t	u16_dat	= 0u;
	uint16_t	u16_di_diag_step	= 0u;
	uint16_t	u16_di_type			= 0u;
	bool_t		b8_di_hold			= BFALSE;
	u16_di_dat	= u16a_dat[0];
		for(u16_idx = 0u; u16_idx < CH_NUM_DI; u16_idx ++)
	{
		
		//u16_di_type = g_st_ram1.config_parameter.u8_sensor_type[u16_idx];   //zht0409   
		u16_di_type = g_st_ram1.config_parameter.input_sensor_control[u16_idx].u8_sensor_type;
		b8_di_hold	= BFALSE;
		u8_ch_fault = g_st_ram1.run_auxiliary_data.st_diag_di_info[u16_idx].u8_fault_confirm;
		if(g_st_ram1.run_display_data.instrument_hmi.u8_mcu_addr == ADDR_MCU_B) //MCU B的处理过程
		{
                        
			if((u16_di_type == DI_TYPE_EMRG_DUAL) || (u16_di_type == DI_TYPE_SAFE_DOOR_DUAL) || (u16_di_type == DI_TYPE_LIMIT_SWITCH_DUAL_2NC))
			{
				/* Extern short diagnosis, Pulse test PT */
				//双通道急停按钮，双通道安全门，双通道限位开关(2NC)需要对脉冲进行检测,并进行报错;
				if(u8_ch_fault == 0u)
				{
					g_st_ram1.run_display_data.instrument_hmi.u32_st_dev_sta &= (uint32_t)U32_CLR_BIT[u16_idx];  
				}
				else
				{
					g_st_ram1.run_display_data.instrument_hmi.u32_st_dev_sta|= (uint32_t)U32_SET_BIT[u16_idx]; //zhou
				}
			}
			else if(u16_di_type == DI_TYPE_SAFE_MAT ) //安全地毯
			{
			  g_st_ram1.run_display_data.instrument_hmi.u32_st_dev_sta &= (uint32_t)U32_CLR_BIT[u16_idx]; //安全地毯，需要检测脉冲，但无需报错 
			}
			else
			{
				/* Disable Pulse test of MCU_B  */
				// 其他类型传感器，如单通道急停按钮，单通道安全门，磁性开关，复位按钮，双手按钮，安全光幕，忽略对MCUB的脉冲检测
				//单通道限位开关,单通道安全光幕,双通道安全门锁(PNP),单通道安全门锁(PNP),磁性开关(PNP),双通道限位开关(1NC+1NO),双通道限位开关(2NC)忽略对MCUB的脉冲检测
				u8_ch_fault = 0u; //脉冲检测故障？置0
				
				/* Input hold , avoid the DI sample value is effected by T1 */
				u16_di_diag_step = dre_get_diag_tx_step();         //获取当前发送的诊断步数
				u16_di_diag_step = u16_di_diag_step & 0x0001u;     //脉冲检测是MCU A/B 为奇偶分别发送，完成错开低电平的考虑
				g_st_ram1.run_display_data.instrument_hmi.u32_st_dev_sta &= (uint32_t)U32_CLR_BIT[u16_idx];
				if(0u == u16_di_diag_step)
				{
					b8_di_hold	= BTRUE;
				}
				else
				{
					;
				}
			}
		}
		else  //MCUA对输入的处理
		{
			if(    (u16_di_type == DI_TYPE_SAFE_CURTAIN)\
					|| (u16_di_type == DI_TYPE_RST_SINGLE)\
			    || (u16_di_type == DI_TYPE_TWO_HAND)\
					|| (u16_di_type == DI_TYPE_MAGNETIC)\
			    || (u16_di_type == DI_TYPE_EMRG_SINGLE)\
			    || (u16_di_type == DI_TYPE_HBS)\
			    || (u16_di_type == DI_TYPE_EMGR_DUAL_NO_PULSE)\
			    || (u16_di_type == DI_TYPE_EMGR_DUAL_NO_BALANCE)\
			    || (u16_di_type == DI_TYPE_MAGNETIC_NO_BALANCE)\
			    || (u16_di_type == DI_TYPE_SAFE_DOOR_SINGLE)\
					|| (u16_di_type == DI_TYPE_SAFE_CURTAIN_SINGLE)\
					|| (u16_di_type == DI_TYPE_SAFE_DOOR_SINGLE_PNP)\
					|| (u16_di_type == DI_TYPE_SAFE_DOOR_DUAL_PNP)\
					|| (u16_di_type == DI_TYPE_MAGNETIC_PNP)\
					|| (u16_di_type == DI_TYPE_LIMIT_SWITCH_SINGLE)\
					|| (u16_di_type == DI_TYPE_LIMIT_SWITCH_DUAL_1NC_1NO))			
			{
				/* 若为安全光幕，单通道复位按钮，双手按钮，磁性开关，单通道急停，心跳传感器，双通道急停无脉冲检测，双通道急停无通道平衡检测，磁性开关无通道平衡检测，安全门单通道,忽略MCUA的脉冲检测*/
				/*若为单通道安全光幕，单通道安全门锁PNP，双通道安全门锁PNP，磁性开关PNP，单通道限位开关，双通道限位开关(1NC+1NO)忽略MCUA的脉冲检测*/
				u8_ch_fault = 0u;
				g_st_ram1.run_display_data.instrument_hmi.u32_st_dev_sta &= (uint32_t)U32_CLR_BIT[u16_idx]; //zhou
			}
			else if(u16_di_type == DI_TYPE_SAFE_MAT)  //安全地毯
			{
				g_st_ram1.run_display_data.instrument_hmi.u32_st_dev_sta &= (uint32_t)U32_CLR_BIT[u16_idx];//若为安全地毯，忽略报错
			}
			else
			{ //若为其他传感器，进行报错
			  if(u8_ch_fault == 0u)
				{
					g_st_ram1.run_display_data.instrument_hmi.u32_st_dev_sta &= (uint32_t)U32_CLR_BIT[u16_idx];
				}
				else
				{
					g_st_ram1.run_display_data.instrument_hmi.u32_st_dev_sta|= (uint32_t)U32_SET_BIT[u16_idx];  //zhou
				}
			}
		}
		/* Handle input data */
		if(b8_di_hold == BTRUE)//若为MCUB进行低电平脉冲诊断,不对当前DI采样值进行判断，只考虑已保存值
		{
			b8_curt_sta = g_st_ram1.run_auxiliary_data.sta_di_filt[u16_idx].b8_thld_sta;
		} 
		else if(((u16_di_dat & U16_SET_BIT[u16_idx]) == 0u) || (u8_ch_fault != 0u))
		{
			b8_curt_sta = BFALSE;//对当前采样值，进行考虑   
		}
		else
		{
			b8_curt_sta = BTRUE;//对当前采样值，进行考虑    不进行考虑zht
		}
		if(u16_di_type == DI_TYPE_DIS)   //若DI无输入
		{
			u8_ch_fault = 0u; //若DI无输入，则DI无通道故障  清楚故障
			g_st_ram1.run_display_data.instrument_hmi.u32_st_dev_sta &= (uint32_t)U32_CLR_BIT[u16_idx]; 
		}			
		else
		{
			;
		}

		/* Input filtering  输入滤波*/
		prg_di_filt(b8_curt_sta, &g_st_ram1.run_auxiliary_data.sta_di_filt[u16_idx], &g_st_ram2.run_auxiliary_data.sta_di_filt[u16_idx]);
		
		if(g_st_ram1.run_auxiliary_data.sta_di_filt[u16_idx].b8_vld_sta == BTRUE)
		{
			
			u16_dat = u16_dat | U16_SET_BIT[u16_idx];//将DI值合并放到，u16_dat
			if(g_st_ram1.run_display_data.instrument_hmi.u8_mcu_addr == ADDR_MCU_A)
			{
				g_st_ram1.run_auxiliary_data.u16_mcua_di |= U16_SET_BIT[u16_idx];
			}
			else
			{
				g_st_ram1.run_auxiliary_data.u16_mcub_di |= U16_SET_BIT[u16_idx];
			}
		}
		else
		{
			u16_dat = u16_dat & U16_CLR_BIT[u16_idx];//将DI值合并放到，u16_dat
				if(g_st_ram1.run_display_data.instrument_hmi.u8_mcu_addr == ADDR_MCU_A)
			{
				g_st_ram1.run_auxiliary_data.u16_mcua_di &= U16_CLR_BIT[u16_idx];
			}
			else
			{
				g_st_ram1.run_auxiliary_data.u16_mcub_di &= U16_CLR_BIT[u16_idx];
			}
		}
				
		
		g_st_ram2.run_auxiliary_data.u16_mcua_di=g_st_ram1.run_auxiliary_data.u16_mcua_di;
		g_st_ram2.run_auxiliary_data.u16_mcub_di=g_st_ram1.run_auxiliary_data.u16_mcub_di;
		g_st_ram2.run_display_data.instrument_hmi.u32_st_dev_sta = 	g_st_ram1.run_display_data.instrument_hmi.u32_st_dev_sta;
		
  }
 	
  prg_get_logic_in(g_st_ram1.run_auxiliary_data.u16_mcua_di,\
	                 g_st_ram1.run_auxiliary_data.u16_mcub_di,\
	                 g_st_ram1.run_display_data.instrument_hmi.u8_dev_fault,\
	                 &g_st_ram1.run_display_data.instrument_hmi,\
	                 &g_st_ram2.run_display_data.instrument_hmi);   //输入通道采集
	}

	/******
 void prg_red_flash(uint32_t u32_st_dev_sta_temp)    //红灯闪烁程序  故障可恢复
	{
		if(g_st_ram1.config_parameter.factory_config_pra.u8_type==6u)//CZSR8903
		{
	    if ((u32_st_dev_sta_temp&0x000FFFFFu)!=0u)    //0x0FFFu  
        {
	            if((u32_st_dev_sta_temp&0x00007C1Fu)!=0u)
	                {//CH1~CH5故障
		                 if(g_st_ram1.run_display_data.instrument_hmi.u8_mcu_addr == ADDR_MCU_A)
	                     	{
													led_set_LED_flash(0x0001);  //zht1 闪
													//bsp_Led_state(0x08,0x01);
													
													g_st_ram1.run_display_data.instrument_hmi.u8_led_sta[0] = LED_FLASH;
													g_st_ram2.run_display_data.instrument_hmi.u8_led_sta[0] = LED_FLASH;
		                    }
		                 else
		                    {
			                    ;
		                    }
		
	                }
	            else
	            {
		             if(g_st_ram1.run_display_data.instrument_hmi.u8_mcu_addr == ADDR_MCU_A)
	                     	{
													
												 g_st_ram1.run_display_data.instrument_hmi.u8_led_sta[0] = LED_FLASH;
												 g_st_ram2.run_display_data.instrument_hmi.u8_led_sta[0] = LED_FLASH;
		                    }
		                 else
		                    {
			                    ;
		                    }
	            }
     if((u32_st_dev_sta_temp&0x000F83E0u)!=0u)   //0x0E38u
	     {//CH6~CH10故障
		      if(g_st_ram1.run_display_data.instrument_hmi.u8_mcu_addr == ADDR_MCU_B)
		        {
							//set_led_flash(0x08);
							led_set_LED_flash(0x0040);   //zht3  闪
							
							g_st_ram1.run_display_data.instrument_hmi.u8_led_sta[0] = LED_FLASH;
							g_st_ram2.run_display_data.instrument_hmi.u8_led_sta[0] = LED_FLASH;
		        }
		     else
		       {
			      ;
		       }
	    }
	   else
	    {
		     if(g_st_ram1.run_display_data.instrument_hmi.u8_mcu_addr == ADDR_MCU_B)
		        {		  
				
							 g_st_ram1.run_display_data.instrument_hmi.u8_led_sta[0] = LED_FLASH;
							 g_st_ram2.run_display_data.instrument_hmi.u8_led_sta[0] = LED_FLASH;
		        }
		     else
		       {
			      ;
		       }
	    }
    }
   else
    {

		
			 g_st_ram1.run_display_data.instrument_hmi.u8_led_sta[0] = LED_FLASH;
			 g_st_ram2.run_display_data.instrument_hmi.u8_led_sta[0] = LED_FLASH;
    }
   }
	else
	{
	   //  CZSR8903    CH1~CH5  00007C1F    CH6~CH10: 000F83E0
	}
}
***************************/

 void prg_run_logic(const uint8_t u8lc_init_val, const uint8_t u8lc_incr_val,const uint16_t *u16p_di_dat, uint16_t *u16p_do_dat)
{
	uint16_t u16p_do_temp =0u;
	uint16_t u16p_do_init  =0u;
	uint16_t u16p_do_adj  =0u;
	uint16_t u16_idx1 = 0u;
	uint16_t u16_idx2 = 0u;

	uint8_t   u8_dev_fault_temp =0u;
	uint32_t  u32_st_dev_sta_temp=0u;
	uint32_t  u32p_temp = 0u;

	
	diag_rcd_run_logic(u8lc_init_val, u8lc_incr_val);//程序顺序执行
	u8_dev_fault_temp =g_st_ram1.run_display_data.instrument_hmi.u8_dev_fault;    //MCU故障
  prg_handle_input(u16p_di_dat, g_st_ram1.run_display_data.instrument_hmi.u8_mcu_addr);//输入处理程序    对应传感器是否脉冲检测
	u32_st_dev_sta_temp = g_st_ram1.run_display_data.instrument_hmi.u32_st_dev_sta|g_st_ram1.run_auxiliary_data.u32_st_dev_sta;  //仅标识输入输出故障
//	g_st_ram1.run_display_data.instrument_hmi.u32_st_dev_sta = u32_st_dev_sta_temp;
//	g_st_ram2.run_display_data.instrument_hmi.u32_st_dev_sta = g_st_ram1.run_display_data.instrument_hmi.u32_st_dev_sta;
	//u32_st_dev_sta_temp = 0x00000001;
	
	for(u16_idx1 = 0u; u16_idx1 < NUM_LOGIC; u16_idx1 ++)
	{
		prg_handle_control(u16_idx1);   //逻辑块逻辑编制
	}
	
	if (u8_dev_fault_temp == 0u)    //看传感器是否直连到输出口。
	{
		  for(u16_idx1 = 0u; u16_idx1 < CH_NUM_DI; u16_idx1 ++)   
		   {
			    for(u16_idx2 = 0u; u16_idx2 < CH_NUM_DO; u16_idx2 ++)  //有对应输出
							    {
//									if ((g_st_ram1.config_parameter.u8_sensor_add[u16_idx1]&U16_SET_BIT[u16_idx2])!= 0u)   //zht0409
										if ((g_st_ram1.config_parameter.input_sensor_control[u16_idx1].u8_sensor_add & U16_SET_BIT[u16_idx2])!= 0u)
										 { //有对应输出
											  if ((g_st_ram1.run_display_data.instrument_hmi.u16_di&U16_SET_BIT[u16_idx1])!= 0u)
											 {
											    u16p_do_temp |= U16_SET_BIT[u16_idx2];
											 }
											 else
                       {
											    u16p_do_temp &= U16_CLR_BIT[u16_idx2];    //	直接相对应		
											 }												 
										 }
									 else
										 {
										    ;
										 }	
									}
			 }
		
	  	for(u16_idx1 = 0u; u16_idx1 < NUM_LOGIC; u16_idx1 ++)    //设定逻辑块 
	     {	       
						for(u16_idx2 = 0u; u16_idx2 < CH_NUM_DO; u16_idx2 ++)  //有对应输出  
						{
						 if((g_st_ram1.config_parameter.logic_control[u16_idx1].u8_function_out_add & U16_SET_BIT[u16_idx2])!= 0u)
							 { //有对应输出
								 if(g_st_ram1.run_display_data.layer_control_value[u16_idx1]==BTRUE)
								 {
										u16p_do_temp |= U16_SET_BIT[u16_idx2];
								 }
								 else
								 {
										u16p_do_temp &= U16_CLR_BIT[u16_idx2];
								 }												 
							 }
						 else
							 {
									;
							 }	
						}
			 //dre_protocol_poll();   //增加SPI通讯可靠性
			 }		 
	}
	else
  {//若存在系统失效
	  u16p_do_temp = 0x0000u;
		
		
	}	
	/* 防止上电误启动程序  主要用于双手按钮的误启动*/
	u32p_temp = comm_tim_calc_diff(&g_st_ram1.run_auxiliary_data.u16_sys_tim_old);
	g_st_ram2.run_auxiliary_data.u16_sys_tim_old=g_st_ram1.run_auxiliary_data.u16_sys_tim_old;
	 if (g_st_ram1.run_auxiliary_data.u16_sys_cnt< PRG_PO_DLY_TIME)
		 { 
						 g_st_ram1.run_auxiliary_data.u16_sys_cnt += (uint16_t)u32p_temp;
						 g_st_ram2.run_auxiliary_data.u16_sys_cnt  =  g_st_ram1.run_auxiliary_data.u16_sys_cnt; 
			       u16p_do_temp = 0x0000u;
		 }
		else
		 {
				 ;
		 }
	
	switch (u8_uart_fuction)
		{
		  case UART_CMD_READ:  //读
	  	case UART_CMD_RUN: //运行
			{
				;
			}break;

			case UART_CMD_WRITE:        //组态状态 输出为0
			{
				/*写RAM必须再操作一次读RAM,才能从命令中出来，回到正常状态*/
				u16p_do_temp=0x0000u;
			}break;
			/*制造控制，需要再操作一次读RAM,才能从命令中出来，回到正常状态*/
			case UART_CMD_RELAYOUTPUT:        //制造控制
			{
				
				u16p_do_temp=0x0003u; //继电器输出   zhou 03
			}break;
			case UART_CMD_ALLOUTPUT:        //制造控制
			{
				u16p_do_temp=0x003Fu; //继电器输出+半导体输出  zhou  3f
			}break;
				case UART_CMD_REDLED_ON:        //制造控制
			{
				  u16p_do_temp=0x0000u;
				//	led_set_red(BTRUE); //红灯ON  周海亭210408
				
			}break;
				case UART_CMD_REDLED_OFF:        //制造控制
			{
				  u16p_do_temp=0x0000u;
					//led_set_red(BFALSE); //红灯OFF  周海亭210408
			}break;
			/*制造控制，需要再操作一次读RAM,才能从命令中出来，回到正常状态*/
			case UART_CMD_FLASH: //FLASH 写操作
			{
				b8_flash_writeflag = BTRUE;
//				prg_write_flash();
//			  u8_uart_fuction=0x00u; //清写FLASH命令
			} break;
			default : break;	
		}

			if((b8_flash_writeflag == BTRUE) && ( b8_uart_txflag == BTRUE))  //发送空闲
			{
				b8_flash_writeflag = BFALSE ;
				prg_write_flash();
				u8_uart_fuction=0x00u; //清写FLASH命令
			}	
			else {
				;
			}			
				
	    if(((g_st_ram1.run_display_data.instrument_hmi.u32_st_dev_sta&0x00F00000u)!=0u)||
         ((g_st_ram1.run_auxiliary_data.u32_st_dev_sta&0x00F00000u)!=0u))
	        {//输出有短路故障，全部断开半导体部分
		          u16p_do_temp &= 0x0003u;    //zhou  0x0003
	        }
    	else
	       {
	       	;
	       }
//		 if	((u16p_do_temp&0x03u)!=0u)
//		    {//继电器有输出，黄灯亮
//			     led_set_yellow(BTRUE);
//					g_st_ram1.run_display_data.instrument_hmi.u16_led |= YELLOW_LED_LIGHT; //黄灯亮 指示
//		      g_st_ram2.run_display_data.instrument_hmi.u16_led |= YELLOW_LED_LIGHT; //黄灯亮 指示
//		    }
//     else
//       {//继电器无输出，黄灯灭
//		      led_set_yellow(BFALSE);
//				  g_st_ram1.run_display_data.instrument_hmi.u16_led &= (uint16_t)~YELLOW_LED_LIGHT; //黄灯灭 指示
//	      	g_st_ram2.run_display_data.instrument_hmi.u16_led &= (uint16_t)~YELLOW_LED_LIGHT; //黄灯灭 指示
//		   }	
				 
		if(g_st_ram1.run_display_data.instrument_hmi.u8_mcu_addr == ADDR_MCU_B)
		  {
			  g_st_ram1.run_auxiliary_data.u16_mcub_do=(uint8_t)u16p_do_temp;
				g_st_ram2.run_auxiliary_data.u16_mcub_do=(uint8_t)u16p_do_temp;
		  }
		else
		  {
			  g_st_ram1.run_auxiliary_data.u16_mcua_do=(uint8_t)u16p_do_temp;
				g_st_ram2.run_auxiliary_data.u16_mcua_do=(uint8_t)u16p_do_temp;
		  }                                                    //u16p_do_temp
		    u16p_do_init= (uint16_t)g_st_ram1.run_auxiliary_data.u16_mcua_do&(uint16_t)g_st_ram1.run_auxiliary_data.u16_mcub_do;  //合并两个MCU的输出
//			//u16p_do_init= (uint16_t)g_st_ram1.run_auxiliary_data.u16_mcua_do&(uint16_t)g_st_ram1.run_auxiliary_data.u16_mcub_do; //数据交互
//		 //*u16p_do_dat  =(uint16_t) (g_st_ram1.run_auxiliary_data.u16_mcua_do&g_st_ram1.run_auxiliary_data.u16_mcub_do); //数据交互

/******************关于继电器均匀输出断开*****************/			
	   g_st_ram1.run_display_data.instrument_hmi.u16_do = u16p_do_init; //均匀输出断开
		 g_st_ram2.run_display_data.instrument_hmi.u16_do = u16p_do_init;
//     if((u16p_do_init & BF_DO_VAL_RO) == BF_DO_VAL_RO)     //BF_DO_VAL_RO = 0x0003u  继电器有输出
//	     {
//		         prg_relay_out_adj(BTRUE, g_st_ram1.run_display_data.instrument_hmi.u8_mcu_addr, 0u, \
//							    &g_st_ram1.run_auxiliary_data.sta_relay,\
//						    	&g_st_ram2.run_auxiliary_data.sta_relay);
//	     }
//	   else
//	     {
//		        prg_relay_out_adj(BFALSE, g_st_ram1.run_display_data.instrument_hmi.u8_mcu_addr, 0u, \
//							    &g_st_ram1.run_auxiliary_data.sta_relay,\
//						    	&g_st_ram2.run_auxiliary_data.sta_relay);
//	     }	
//      u16p_do_adj =	 u16p_do_init;
//			u16p_do_adj &= (uint16_t)~(uint16_t)BF_DO_VAL_RO;
//			u16p_do_adj |= (uint16_t)g_st_ram1.run_auxiliary_data.sta_relay.u16_adj_rslt;
//			*u16p_do_dat  =(uint16_t)u16p_do_adj;  
/************************************************************/	
	   
		 *u16p_do_dat  = u16p_do_init; 
    //   *u16p_do_dat  = 0x0F;		 
		 
	if (u8_dev_fault_temp == 0u)  //MCU没有故障 0
	{
		 //红灯灭
		 g_st_ram1.run_display_data.instrument_hmi.u8_led_sta[5] = LED_OFF; //红灯灭 指示
		 g_st_ram2.run_display_data.instrument_hmi.u8_led_sta[5] = LED_OFF; //红灯灭 指示
	}		
	else
	{
		g_st_ram1.run_display_data.instrument_hmi.u8_led_sta[5] = LED_ON; //红灯亮指示
		g_st_ram2.run_display_data.instrument_hmi.u8_led_sta[5] = LED_ON; //红灯亮 指示	
	}

	if((g_st_ram1.run_display_data .instrument_hmi .u32_st_dev_sta & 0x000FFFFFu)!=0u) //输入有故障
	{
		if(g_st_ram1.run_display_data.instrument_hmi.u8_mcu_addr == ADDR_MCU_A)  //MCUA 对应LED1-5 CH1-CH5
		{			
		   for(u16_idx1=0; u16_idx1<5; u16_idx1++ )  
		   {	
			   if( ( (g_st_ram1.run_display_data.instrument_hmi.u32_st_dev_sta >> u16_idx1)&0x00000401u)!= 0u )  //CH1-CH5
			     {
				   g_st_ram1.run_display_data.instrument_hmi.u8_led_sta[u16_idx1] = LED_FLASH; //指示灯闪烁
		       g_st_ram2.run_display_data.instrument_hmi.u8_led_sta[u16_idx1] = LED_FLASH; //指示灯闪烁
					 g_st_ram1.run_auxiliary_data.b8_led_flashflag[u16_idx1]=BTRUE; //指示灯闪烁标志
					 g_st_ram2.run_auxiliary_data.b8_led_flashflag[u16_idx1]=BTRUE; //指示灯闪烁标志
			     }			 
				 	/*有输入且无闪标志*/
					 if((((g_st_ram1.run_display_data.instrument_hmi.u16_di>>u16_idx1)&0x0001u)==0x0001u)&&(g_st_ram1.run_auxiliary_data.b8_led_flashflag[u16_idx1]==BFALSE)) 
					{
						g_st_ram1.run_display_data.instrument_hmi.u8_led_sta[u16_idx1] = LED_ON;
						g_st_ram2.run_display_data.instrument_hmi.u8_led_sta[u16_idx1] = LED_ON;
					}
					/*没有输入且无闪标志 */
					else if((((g_st_ram1.run_display_data.instrument_hmi.u16_di>>u16_idx1)&0x0001u)!=0x0001u)&&(g_st_ram1.run_auxiliary_data.b8_led_flashflag[u16_idx1]==BFALSE))  
					{ 
						g_st_ram1.run_display_data.instrument_hmi.u8_led_sta[u16_idx1] = LED_OFF;
						g_st_ram2.run_display_data.instrument_hmi.u8_led_sta[u16_idx1] = LED_OFF;						
					}
					else
					{
						;
					}
		   }
	  }
		else         //MCUB   对应LED6-10 CH6-CH10     
		{	
			 for(u16_idx1=0; u16_idx1<5; u16_idx1++ )  //
		   {	
			  if(((g_st_ram1.run_display_data.instrument_hmi.u32_st_dev_sta >> u16_idx1)&0x00008020u)!= 0u )  //CH6 - CH10
			   {
				   g_st_ram1.run_display_data.instrument_hmi.u8_led_sta[u16_idx1] = LED_FLASH; //指示灯闪烁
		       g_st_ram2.run_display_data.instrument_hmi.u8_led_sta[u16_idx1] = LED_FLASH; //指示灯闪烁
           g_st_ram1.run_auxiliary_data.b8_led_flashflag[u16_idx1]=BTRUE; //指示灯闪烁标志
					 g_st_ram2.run_auxiliary_data.b8_led_flashflag[u16_idx1]=BTRUE; //指示灯闪烁标志					 
			   }
		   }
			 for(u16_idx1=5; u16_idx1<10; u16_idx1++ ) 
			 {
					/*有输入且无闪标志*/
					if((((g_st_ram1.run_display_data.instrument_hmi.u16_di>>u16_idx1)&0x0001u)==0x0001u)&&(g_st_ram1.run_auxiliary_data.b8_led_flashflag[u16_idx1-5]==BFALSE))
					{
						g_st_ram1.run_display_data.instrument_hmi.u8_led_sta[u16_idx1-5] = LED_ON;
						g_st_ram2.run_display_data.instrument_hmi.u8_led_sta[u16_idx1-5] = LED_ON;
					}
					/*没有输入且无闪标志 */
					else if((((g_st_ram1.run_display_data.instrument_hmi.u16_di>>u16_idx1)&0x0001u)!=0x0001u)&&(g_st_ram1.run_auxiliary_data.b8_led_flashflag[u16_idx1-5]==BFALSE))
					{ 
						g_st_ram1.run_display_data.instrument_hmi.u8_led_sta[u16_idx1-5] = LED_OFF;
						g_st_ram2.run_display_data.instrument_hmi.u8_led_sta[u16_idx1-5] = LED_OFF;						
					}
					else
					{
						;
					}					
			  }			 
				
		}	
  }	
  else  //输入无故障
  {  
     if(g_st_ram1.run_display_data.instrument_hmi.u8_mcu_addr == ADDR_MCU_A)  //MCUA 对应LED1-5 CH1-CH5
		 {
		   for(u16_idx1=0; u16_idx1<5; u16_idx1++ ) 
			 {
			    if((((g_st_ram1.run_display_data.instrument_hmi.u16_di>>u16_idx1)&0x0001u)==0x0001u) \
						&&(g_st_ram1.run_auxiliary_data.b8_led_flashflag[u16_idx1]==BFALSE))  //有输入且无闪标志
					{
					    g_st_ram1.run_display_data.instrument_hmi.u8_led_sta[u16_idx1] = LED_ON;
					  	g_st_ram2.run_display_data.instrument_hmi.u8_led_sta[u16_idx1] = LED_ON;
					}
					else if((((g_st_ram1.run_display_data.instrument_hmi.u16_di>>u16_idx1)&0x0001u)!=0x0001u) \
						      &&(g_st_ram1.run_auxiliary_data.b8_led_flashflag[u16_idx1]==BFALSE)) //没有输入且无闪标志   
					{ 
						  g_st_ram1.run_display_data.instrument_hmi.u8_led_sta[u16_idx1] = LED_OFF;
					  	g_st_ram2.run_display_data.instrument_hmi.u8_led_sta[u16_idx1] = LED_OFF;						
					}
					else
					{
						;
					}			
			 }
		 }
		 else    //MCUB CH6 - CH10
		 {
			  for(u16_idx1=5; u16_idx1<10; u16_idx1++ ) 
			 {
			    if((((g_st_ram1.run_display_data.instrument_hmi.u16_di>>u16_idx1)&0x0001u)==0x0001u)&&(g_st_ram1.run_auxiliary_data.b8_led_flashflag[u16_idx1-5]==BFALSE))
					{
					    g_st_ram1.run_display_data.instrument_hmi.u8_led_sta[u16_idx1-5] = LED_ON;
					  	g_st_ram2.run_display_data.instrument_hmi.u8_led_sta[u16_idx1-5] = LED_ON;
					}
					else if((((g_st_ram1.run_display_data.instrument_hmi.u16_di>>u16_idx1)&0x0001u)!=0x0001u)&&(g_st_ram1.run_auxiliary_data.b8_led_flashflag[u16_idx1-5]==BFALSE))
					{ 
						  g_st_ram1.run_display_data.instrument_hmi.u8_led_sta[u16_idx1-5] = LED_OFF;
					  	g_st_ram2.run_display_data.instrument_hmi.u8_led_sta[u16_idx1-5] = LED_OFF;						
					}
					else
					{
						;
					}			
			 }		 
		 }	
  }	
	for(u16_idx1=0u; u16_idx1<	CH_NUM_DI; u16_idx1++ )  //有复位按钮且为高电平清除闪标志
	{
    if (g_st_ram1.config_parameter.input_sensor_control[u16_idx1].u8_sensor_type==DI_TYPE_RST_SINGLE)
		{
			if(((g_st_ram1.run_display_data.instrument_hmi.u16_di>>u16_idx1)&0x0001u)==0x0001u)
			{
				g_st_ram1.run_auxiliary_data.b8_led_flashflag[0]=BFALSE;
				g_st_ram1.run_auxiliary_data.b8_led_flashflag[1]=BFALSE;
				g_st_ram1.run_auxiliary_data.b8_led_flashflag[2]=BFALSE;
				g_st_ram1.run_auxiliary_data.b8_led_flashflag[3]=BFALSE;
				g_st_ram1.run_auxiliary_data.b8_led_flashflag[4]=BFALSE;
				
				g_st_ram2.run_auxiliary_data.b8_led_flashflag[0]=BFALSE;
				g_st_ram2.run_auxiliary_data.b8_led_flashflag[1]=BFALSE;
				g_st_ram2.run_auxiliary_data.b8_led_flashflag[2]=BFALSE;
				g_st_ram2.run_auxiliary_data.b8_led_flashflag[3]=BFALSE;
				g_st_ram2.run_auxiliary_data.b8_led_flashflag[4]=BFALSE;
			}
		}
		else
		{
		 ;
		}		
	}

}

 void led_control(const uint8_t u8lc_init_val, const uint8_t u8lc_incr_val)
 {
	 uint8_t  u8_idx;
	 uint8_t  u8_led_on = 0u;
	 uint8_t  u8_led_flash = 0u;
   diag_rcd_run_logic(u8lc_init_val, u8lc_incr_val);//程序顺序执行
	 
	 for(u8_idx=0; u8_idx<6; u8_idx++)
	 {
		  if(g_st_ram1.run_display_data.instrument_hmi.u8_led_sta[u8_idx] == 0u)
	      {
					 u8_led_on &= U16_CLR_BIT[u8_idx];
					 u8_led_flash &= U16_CLR_BIT[u8_idx];
	      }
	    else if (g_st_ram1.run_display_data.instrument_hmi.u8_led_sta[u8_idx] == 1u)
	      {
	         u8_led_on |= U16_SET_BIT[u8_idx];
					 u8_led_flash &= U16_CLR_BIT[u8_idx];
	      }
			else if(g_st_ram1.run_display_data.instrument_hmi.u8_led_sta[u8_idx] == 2u)
			 {
			    u8_led_flash |= U16_SET_BIT[u8_idx]; 
				  u8_led_on &= U16_CLR_BIT[u8_idx];
			 }
			else
			{
				;
			}
		}
	   //u8_led_on=0x00;
		 //u8_led_flash=0x3F;
			bsp_Led_state(u8_led_on,u8_led_flash); //LED灯亮灭
		  led_set_LED_flash(u8_led_flash);       //LED灯闪烁
}
		 


static void prg_handle_control(uint16_t u16_idx)
{
	//uint16_t u16p_temp = 0u;
	uint32_t u32p_temp = 0u;
	uint8_t u8p_temp = 0u;
	uint8_t u8p_temp1 = 0u;
	uint8_t u8p_temp2 = 0u;
  uint8_t u8p_temp3 = 0u;
	uint16_t u16p_temp1 = 0u;
	bool_t	b8_temp1 = BFALSE;
	bool_t	b8_temp2 = BFALSE;
	bool_t	b8_temp3 = BFALSE;
	bool_t	b8_temp4 = BFALSE;
	switch (g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_type)
	{
		case LOGIC_DIS: break;
		case LOGIC_AANDB: //A&B
		     {  
					 u8p_temp = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in1_add;  //逻辑块的输入地址1
					 b8_temp1 = prg_get_control_in(u8p_temp);
					 u8p_temp = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in2_add;  //逻辑块的输入地址2
					 b8_temp2 = prg_get_control_in(u8p_temp);
					 b8_temp3 = prg_logic_AandB(b8_temp1,b8_temp2);
					 g_st_ram1.run_display_data.layer_control_value[u16_idx]=b8_temp3;
		     }break;
		case LOGIC_AANDBANDC: //A&B&C
	    	{
					 u8p_temp = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in1_add;
					 b8_temp1 = prg_get_control_in(u8p_temp);
					 u8p_temp = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in2_add;
					 b8_temp2 = prg_get_control_in(u8p_temp);
					 u8p_temp = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in3_add;
					 b8_temp3 = prg_get_control_in(u8p_temp);
					 b8_temp4 = prg_logic_AandBandC( b8_temp1,  b8_temp2, b8_temp3);
					 g_st_ram1.run_display_data.layer_control_value[u16_idx]=b8_temp4;
	    	}break;
		case LOGIC_AORB: //A|B
	    	{
				   u8p_temp = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in1_add;
					 b8_temp1 = prg_get_control_in(u8p_temp);
					 u8p_temp = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in2_add;
					 b8_temp2 = prg_get_control_in(u8p_temp);
					 b8_temp3 = prg_logic_AorB(b8_temp1,b8_temp2);
					 g_st_ram1.run_display_data.layer_control_value[u16_idx]=b8_temp3;
	    	}break;
		case LOGIC_AORBORC: //A||B||C
	    	{
					 u8p_temp = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in1_add;
					 b8_temp1 = prg_get_control_in(u8p_temp);
					 u8p_temp = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in2_add;
					 b8_temp2 = prg_get_control_in(u8p_temp);
					 u8p_temp = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in3_add;
					 b8_temp3 = prg_get_control_in(u8p_temp);
					 b8_temp4 = prg_logic_AorBorC( b8_temp1,  b8_temp2, b8_temp3);
					 g_st_ram1.run_display_data.layer_control_value[u16_idx]=b8_temp4;
	    	}break;
		case LOGIC_NOTA: //!A
	    	{
					 u8p_temp = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in1_add;
					 b8_temp1 = prg_get_control_in(u8p_temp);
					 b8_temp2 = prg_logic_notA( b8_temp1);
					 g_st_ram1.run_display_data.layer_control_value[u16_idx]=b8_temp2;
	    	}break;
   case LOGIC_TWOHAND: //two hand
	    	{  
					 u32p_temp = comm_tim_calc_diff(&g_st_ram1.run_auxiliary_data.u16_sys_tim_old);
					 g_st_ram2.run_auxiliary_data.u16_sys_tim_old=g_st_ram1.run_auxiliary_data.u16_sys_tim_old;
				   u8p_temp1 = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in1_add;
					 b8_temp1 = prg_get_control_in(u8p_temp1);
					 g_st_ram1.run_auxiliary_data.sta_di_control[u8p_temp1-101u].b8_vld_sta=b8_temp1;
					 g_st_ram2.run_auxiliary_data.sta_di_control[u8p_temp1-101u].b8_vld_sta=b8_temp1;
					 u8p_temp2 = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in2_add;
					 b8_temp2 = prg_get_control_in(u8p_temp2);
					 g_st_ram1.run_auxiliary_data.sta_di_control[u8p_temp2-101u].b8_vld_sta=b8_temp2;
					 g_st_ram2.run_auxiliary_data.sta_di_control[u8p_temp2-101u].b8_vld_sta=b8_temp2;
					 if (g_st_ram1.run_auxiliary_data.u16_sys_cnt< PRG_PO_DLY_TIME)
						 { //防止双手上电启动程序
						 g_st_ram1.run_auxiliary_data.u16_sys_cnt += (uint16_t)u32p_temp;
						 g_st_ram2.run_auxiliary_data.u16_sys_cnt  =  g_st_ram1.run_auxiliary_data.u16_sys_cnt; 
						 g_st_ram1.run_auxiliary_data.sta_di_control[u8p_temp2-101u].b8_thld_sta=BTRUE;
					   g_st_ram2.run_auxiliary_data.sta_di_control[u8p_temp2-101u].b8_thld_sta=BTRUE;
					 }
					 else
					 {
						 ;
					 }
					 b8_temp3 = g_st_ram1.run_display_data.layer_control_value[u16_idx]; //取得当前模块的输出
					 b8_temp4 = prg_handle_two_hand(b8_temp3,&g_st_ram1.run_auxiliary_data.sta_di_control[u8p_temp1-101],\
					                                &g_st_ram1.run_auxiliary_data.sta_di_control[u8p_temp2-101],\
                                          &g_st_ram2.run_auxiliary_data.sta_di_control[u8p_temp1-101],\
					                                &g_st_ram2.run_auxiliary_data.sta_di_control[u8p_temp2-101]);
					 g_st_ram1.run_display_data.layer_control_value[u16_idx]=b8_temp4;
	    	}break;
	 case LOGIC_RST_LTH: //复位上升沿
		  {
				u8p_temp1 = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in1_add; //输入线地址
				b8_temp1 = prg_get_control_in(u8p_temp1);//取得输入的值
				u8p_temp2 = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in2_add; //复位线地址
				b8_temp2 = prg_get_reset_in(u8p_temp2);//取得当前复位线的值
				
				g_st_ram1.run_auxiliary_data.sta_di_control[u8p_temp2-101u].b8_vld_sta=b8_temp2;
				g_st_ram2.run_auxiliary_data.sta_di_control[u8p_temp2-101u].b8_vld_sta=b8_temp2;
				
				b8_temp3 = g_st_ram1.run_display_data.layer_control_value[u16_idx]; //取得当前复位模块的输出
				b8_temp4 = prg_handle_rstltoh(b8_temp1,b8_temp3,&g_st_ram1.run_auxiliary_data.sta_di_control[u8p_temp2-101u],\
				                              &g_st_ram2.run_auxiliary_data.sta_di_control[u8p_temp2-101u]);
				g_st_ram1.run_display_data.layer_control_value[u16_idx]=b8_temp4;
				g_st_ram2.run_display_data.layer_control_value[u16_idx]=b8_temp4;
			}break;
		
	 case LOGIC_RST_HTL: //复位下降沿
		  {
				u8p_temp1 = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in1_add; //输入线地址
				b8_temp1 = prg_get_control_in(u8p_temp1);//取得输入的值
				u8p_temp2 = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in2_add; //复位线地址
				b8_temp2 = prg_get_reset_in(u8p_temp2);//取得当前复位线的值
				g_st_ram1.run_auxiliary_data.sta_di_control[u8p_temp2-101u].b8_vld_sta=b8_temp2;
				g_st_ram2.run_auxiliary_data.sta_di_control[u8p_temp2-101u].b8_vld_sta=b8_temp2;
				b8_temp3 = g_st_ram1.run_display_data.layer_control_value[u16_idx]; //取得当前复位模块的输出
				b8_temp4 = prg_handle_rsthtol(b8_temp1,b8_temp3, &g_st_ram1.run_auxiliary_data.sta_di_control[u8p_temp2-101u],\
				                                                 &g_st_ram2.run_auxiliary_data.sta_di_control[u8p_temp2-101u]);
				g_st_ram1.run_display_data.layer_control_value[u16_idx]=b8_temp4;
				
			}break;
	 case LOGIC_RST_HTH: //复位高电平
		  {
				u8p_temp1 = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in1_add; //输入线地址
				b8_temp1 = prg_get_control_in(u8p_temp1);//取得输入的值
				u8p_temp2 = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in2_add; //复位线地址
				b8_temp2 = prg_get_control_in(u8p_temp2);//取得当前复位线的值
				g_st_ram1.run_auxiliary_data.sta_di_control[u8p_temp2-101u].b8_vld_sta=b8_temp2;
				g_st_ram2.run_auxiliary_data.sta_di_control[u8p_temp2-101u].b8_vld_sta=b8_temp2;
				b8_temp3 = g_st_ram1.run_display_data.layer_control_value[u16_idx]; //取得当前复位模块的输出
				b8_temp4 = prg_handle_rsthtoh(b8_temp1,b8_temp3,&g_st_ram1.run_auxiliary_data.sta_di_control[u8p_temp2-101],\
				                                                &g_st_ram2.run_auxiliary_data.sta_di_control[u8p_temp2-101]);
				g_st_ram1.run_display_data.layer_control_value[u16_idx]=b8_temp4;
				
			}break;
	 case LOGIC_DELAYON: //延时ON
		  {
			  u8p_temp1 = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in1_add; //输入线地址
				b8_temp1 = prg_get_control_in(u8p_temp1);//取得输入的值
				u16p_temp1 = g_st_ram1.config_parameter.logic_control[u16_idx].u16_time_pra; //取时间参数			
				b8_temp2 = g_st_ram1.run_display_data.layer_control_value[u16_idx];    //逻辑块的输出值
				u32p_temp = u16p_temp1 *10u ;  //时间参数是重新定义的，因此需要缩小
        b8_temp3= prg_handle_delay_on( b8_temp1, b8_temp2,u32p_temp,  &g_st_ram1.run_auxiliary_data.delay_control[u16_idx], \
				&g_st_ram2.run_auxiliary_data.delay_control[u16_idx]);		
				g_st_ram1.run_display_data.layer_control_value[u16_idx]=b8_temp3;
			}break;
	case LOGIC_DELAYOFF: //延时OFF
		  {
				u8p_temp1 = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in1_add; //输入线地址
				b8_temp1 = prg_get_control_in(u8p_temp1);//取得输入的值
				u16p_temp1 = g_st_ram1.config_parameter.logic_control[u16_idx].u16_time_pra; //取时间参数

				b8_temp2 = g_st_ram1.run_display_data.layer_control_value[u16_idx];
				u32p_temp = u16p_temp1*10u ;  //时间参数是重新定义的，因此需要缩小
        b8_temp3= prg_handle_delay_off( b8_temp1, b8_temp2 ,u32p_temp,  &g_st_ram1.run_auxiliary_data.delay_control[u16_idx], \
				&g_st_ram2.run_auxiliary_data.delay_control[u16_idx]);		
				g_st_ram1.run_display_data.layer_control_value[u16_idx]=b8_temp3;
				
			}break;
		case LOGIC_AXNORB: //同或
		  {
				   u8p_temp = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in1_add;
					 b8_temp1 = prg_get_control_in(u8p_temp);
					 u8p_temp = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in2_add;
					 b8_temp2 = prg_get_control_in(u8p_temp);
					 b8_temp3 = prg_logic_AxnorB(b8_temp1,b8_temp2);
					 g_st_ram1.run_display_data.layer_control_value[u16_idx]=b8_temp3;
				
			}break;
		case LOGIC_AXORB: //异或
		  {
				   u8p_temp = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in1_add;
					 b8_temp1 = prg_get_control_in(u8p_temp);
					 u8p_temp = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in2_add;
					 b8_temp2 = prg_get_control_in(u8p_temp);
					 b8_temp3 = prg_logic_AxorB(b8_temp1,b8_temp2);
					 g_st_ram1.run_display_data.layer_control_value[u16_idx]=b8_temp3;
				
			}break;
			
	  //添加 心跳监控 zhouht		
		case LOGIC_HBS:
		{
     u8p_temp1 = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in1_add; //输入线地址
		 b8_temp1 = prg_get_control_in(u8p_temp1);//取得输入的值	
					
//     u8p_temp2 = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in3_add; //取频率参数低位
//		 u8p_temp3 = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in4_add; //取频率参数高位
//		 u32p_temp = (u8p_temp2 + (u8p_temp3*256u))*5u ;  //频率参数是重新定义的，
			
		 u16p_temp1  = g_st_ram1.config_parameter.logic_control[u16_idx].u16_time_pra;   //取频率参数
		 u32p_temp = u16p_temp1*10u ;  //频率参数是重新定义的，
					
		 b8_temp2= prg_heart_beat_monitor( b8_temp1, u32p_temp, &g_st_ram1.run_auxiliary_data.delay_control[u16_idx], \
				                                                    &g_st_ram2.run_auxiliary_data.delay_control[u16_idx]);
		
		 g_st_ram1.run_display_data.layer_control_value[u16_idx]=b8_temp2;		
		}break;
		
	  //频率输出 zht
		case LOGIC_FRE_OUT:
		{
					
			u8p_temp1 = g_st_ram1.config_parameter.logic_control[u16_idx].u8_function_in1_add; //输入线地址
		  b8_temp1 = prg_get_control_in(u8p_temp1);//取得输入的值
				
			u16p_temp1 = g_st_ram1.config_parameter.logic_control[u16_idx].u16_time_pra; //取频率参数
			u32p_temp = u16p_temp1*10u ;  //频率参数是重新定义的，  是否需要转化?

      b8_temp2 = g_st_ram1.run_display_data.layer_control_value[u16_idx]; //取得当前复位模块的输出
			
			g_st_ram1.run_auxiliary_data.delay_control[u16_idx].b8_res[1]=b8_temp2;
			g_st_ram2.run_auxiliary_data.delay_control[u16_idx].b8_res[1]=b8_temp2;
			
			b8_temp3 = prg_fre_out( b8_temp1, u32p_temp,  &g_st_ram1.run_auxiliary_data.delay_control[u16_idx], \
				                                            &g_st_ram2.run_auxiliary_data.delay_control[u16_idx]);
			
			g_st_ram1.run_display_data.layer_control_value[u16_idx]=b8_temp3;	
			g_st_ram2.run_display_data.layer_control_value[u16_idx]=b8_temp3;
		}break;
			
 default : break;	
	}
	g_st_ram2.run_display_data.layer_control_value[u16_idx]=g_st_ram1.run_display_data.layer_control_value[u16_idx];
}

bool_t  prg_get_control_in(uint8_t address)    //zhou ?
{
	bool_t	b8_temp = BFALSE;
          if(address  >100u )
					 {
							  if ((g_st_ram1.run_display_data.instrument_hmi.u16_di&U16_SET_BIT[address-101u])!= 0u)
								{
									b8_temp=BTRUE;
								}
								else
								{
								  b8_temp = BFALSE;
								}
					 }
					 else if ((address > 0u) && (address < 100u))/**/
					 {
						 b8_temp = g_st_ram1.run_display_data.layer_control_value[address-1u];  
					 }
				  else
					{
						//
					}		
		return b8_temp;   //返回一个逻辑块的输出值
}
	
/*复位按钮输入值判断*/
/*排除程序第一次运行u16_di值为0,导致复位按钮上一次值发生变化,故增加复位按钮单独的判断语句*/
bool_t  prg_get_reset_in(uint8_t address)
{
	bool_t	b8_temp = BFALSE;
          if(address  >100u )
					 {
							  if ((g_st_ram1.run_auxiliary_data.u16_mcua_di & U16_SET_BIT[address-101u])!= 0u)
								{
									b8_temp=BTRUE;
								}
								else
								{
								  b8_temp = BFALSE;
								}
					 }
				  else
					{
						;
					}		
		return b8_temp;
}
                                                                               //仪表可视化数据
static void prg_get_logic_in(uint16_t u16_mcua_di, uint16_t u16_mcub_di,uint8_t u8_dev_fault,instr_hmi_t *stp_sta_dat,instr_hmi_t *stp_sta_dat_bak)
{
	uint16_t	u16_idx		= 0u;
	uint32_t	u32_tmp_dat	= 0u;
	if (u8_dev_fault == 0u)
	   {
		    for(u16_idx = 0u; u16_idx < CH_NUM_DI; u16_idx ++)
	        {
		          switch(g_st_ram1.config_parameter.input_sensor_control[u16_idx].u8_sensor_type)   //zht0409   g_st_ram1.config_parameter.u8_sensor_type[u16_idx]
							{
								case DI_TYPE_DIS:
								 {
									stp_sta_dat->u16_di &= U16_CLR_BIT[u16_idx]; 
								 }break;
                case DI_TYPE_EMRG_DUAL:              //急停按钮双通道
								case DI_TYPE_SAFE_DOOR_DUAL:	       //安全门双通道
								case DI_TYPE_SAFE_CURTAIN:           //安全光幕
								case DI_TYPE_EMGR_DUAL_NO_PULSE:     //急停按钮双通道忽略脉冲
								case DI_TYPE_SAFE_MAT:               //安全地毯
								case DI_TYPE_SAFE_DOOR_DUAL_PNP:     //安全门双通道_PNP
								case DI_TYPE_LIMIT_SWITCH_DUAL_2NC:	 //限位开关双通道_2NC
								{
									if ((u16_mcua_di & U16_SET_BIT[u16_idx])==(u16_mcub_di & U16_SET_BIT[u16_idx]))
									{
										 if((u16_mcua_di & U16_SET_BIT[u16_idx])!= 0u)   //输入有值
										 {
											 if ((stp_sta_dat->u32_st_dev_sta&U32_SET_BIT[u16_idx+CH_NUM_DI])==0u)  //无错
											 {
												 stp_sta_dat->u16_di |= U16_SET_BIT[u16_idx]; 
											 }
											 else
											 {
												stp_sta_dat->u16_di &= U16_CLR_BIT[u16_idx]; 
											 }										 										 
										 }
										 else
											 {  //只有当输入都为0的时候才进行不同步的故障消除
										    stp_sta_dat->u16_di &= U16_CLR_BIT[u16_idx]; 
                        stp_sta_dat->u32_st_dev_sta &= U32_CLR_BIT[u16_idx+CH_NUM_DI]; 	 //zhou		//故障清除												 
										   }
											  
											  //stp_sta_dat->u16_di &= U16_CLR_BIT[u16_idx]; 
                       // stp_sta_dat->u32_st_dev_sta &= U32_CLR_BIT[u16_idx+CH_NUM_DI]; 	 //故障清除			
											 
											  g_st_ram1.run_auxiliary_data.sta_di_vote[u16_idx].u16_cnt		= 0u;
			                  g_st_ram1.run_auxiliary_data.sta_di_vote[u16_idx].u16_tim_old	= comm_tim_get_currt_cnt();
										    g_st_ram2.run_auxiliary_data.sta_di_vote[u16_idx].u16_cnt		= 0u;
			                  g_st_ram2.run_auxiliary_data.sta_di_vote[u16_idx].u16_tim_old	= g_st_ram1.run_auxiliary_data.sta_di_vote[u16_idx].u16_tim_old;
									}
									else  //输入有错
									{
										  u32_tmp_dat	= comm_tim_calc_diff(&g_st_ram1.run_auxiliary_data.sta_di_vote[u16_idx].u16_tim_old);
			/* Extern fault confirm   外部故障确认*/
			                if(g_st_ram1.run_auxiliary_data.sta_di_vote[u16_idx].u16_cnt < CH_EX_ERR_FILT)  //5S 50000u
										    {
				                  g_st_ram1.run_auxiliary_data.sta_di_vote[u16_idx].u16_cnt += (uint16_t)u32_tmp_dat;
		              	    }
									    else
										   {
				                 stp_sta_dat->u16_di &= U16_CLR_BIT[u16_idx]; 
												 stp_sta_dat->u32_st_dev_sta |= U32_SET_BIT[u16_idx+CH_NUM_DI];  //zhou
			                 }
			/* Signal asynchronism confirm   信号差异故障确认*/
		           	     if(CH_EX_ASYNC_FILT < g_st_ram1.run_auxiliary_data.sta_di_vote[u16_idx].u16_cnt)
											 {
				               stp_sta_dat->u16_di &= U16_CLR_BIT[u16_idx]; 
                       //stp_sta_dat->u32_st_dev_sta |= U16_SET_BIT[u16_idx+6];  												
		            	     }
							   	   else
										   {
				                 ;
			                 } 
											 g_st_ram2.run_auxiliary_data.sta_di_vote[u16_idx].u16_cnt = g_st_ram1.run_auxiliary_data.sta_di_vote[u16_idx].u16_cnt;
											 g_st_ram2.run_auxiliary_data.sta_di_vote[u16_idx].u16_tim_old	= g_st_ram1.run_auxiliary_data.sta_di_vote[u16_idx].u16_tim_old;
								  }		
							  }break;	
								case DI_TYPE_EMGR_DUAL_NO_BALANCE:     //急停按钮双通道忽略通道平衡检测
								{
									if((u16_mcua_di & U16_SET_BIT[u16_idx])==(u16_mcub_di & U16_SET_BIT[u16_idx]))
									{
										if((u16_mcua_di & U16_SET_BIT[u16_idx])!= 0u)   //输入有值
										{
											if((stp_sta_dat->u32_st_dev_sta&U32_SET_BIT[u16_idx+CH_NUM_DI])==0u)  //无错
											{
												stp_sta_dat->u16_di |= U16_SET_BIT[u16_idx];   //置输入为1
											}
											else
											{
												stp_sta_dat->u16_di &= U16_CLR_BIT[u16_idx];    //置输入为0
											}										 										 
										}
										else
										{
											stp_sta_dat->u16_di &= U16_CLR_BIT[u16_idx];     //置输入为0
										}
									}
									else
									{
										stp_sta_dat->u16_di &= U16_CLR_BIT[u16_idx];    //置输入为0
									}
								}break;
								case DI_TYPE_RST_SINGLE:       //单通道复位按钮
								case DI_TYPE_EMRG_SINGLE:      //急停按钮单通道
								case DI_TYPE_HBS:              //增加心跳传感器
								case DI_TYPE_SAFE_DOOR_SINGLE: //安全门单通道
								case DI_TYPE_SAFE_CURTAIN_SINGLE://安全光幕单通道
								case DI_TYPE_SAFE_DOOR_SINGLE_PNP://安全门单通道_PNP
								case DI_TYPE_LIMIT_SWITCH_SINGLE://限位开关
								{
                      if((u16_mcua_di & U16_SET_BIT[u16_idx])!= 0u)
										 {
											 stp_sta_dat->u16_di |= U16_SET_BIT[u16_idx]; 
										 }
										 else
										 {
										  stp_sta_dat->u16_di &= U16_CLR_BIT[u16_idx]; 
										 }
								}break;
								case DI_TYPE_MAGNETIC:  //磁性开关
								case DI_TYPE_TWO_HAND:  // 双手按钮
								case DI_TYPE_LIMIT_SWITCH_DUAL_1NC_1NO://限位开关_1NC_1NO
							  case DI_TYPE_MAGNETIC_PNP://磁性开关_PNP
								    {
		                   if(((u16_mcua_di & U16_SET_BIT[u16_idx])!= 0u) && ((u16_mcub_di & U16_SET_BIT[u16_idx])== 0u))  //输出“1”
												 {
													 if ((stp_sta_dat->u32_st_dev_sta&U32_SET_BIT[u16_idx+CH_NUM_DI])==0u) 
													 {
		                       stp_sta_dat->u16_di |= U16_SET_BIT[u16_idx]; 
                           stp_sta_dat->u32_st_dev_sta &= U32_CLR_BIT[u16_idx+CH_NUM_DI];   
													 }
													 else 
													 {
													  stp_sta_dat->u16_di &= U16_CLR_BIT[u16_idx];  
													 }
                           g_st_ram1.run_auxiliary_data.sta_di_vote[u16_idx].u16_cnt		= 0u;
			                     g_st_ram1.run_auxiliary_data.sta_di_vote[u16_idx].u16_tim_old	= comm_tim_get_currt_cnt();
													 g_st_ram2.run_auxiliary_data.sta_di_vote[u16_idx].u16_cnt		= 0u;
			                     g_st_ram2.run_auxiliary_data.sta_di_vote[u16_idx].u16_tim_old	= g_st_ram1.run_auxiliary_data.sta_di_vote[u16_idx].u16_tim_old;
		                    }
												 else if(((u16_mcua_di & U16_SET_BIT[u16_idx])== 0u) && ((u16_mcub_di & U16_SET_BIT[u16_idx])!= 0u))  //输出“0”
												{
			                    stp_sta_dat->u16_di &= U16_CLR_BIT[u16_idx]; 
													stp_sta_dat->u32_st_dev_sta &= U32_CLR_BIT[u16_idx+CH_NUM_DI]; 
													g_st_ram1.run_auxiliary_data.sta_di_vote[u16_idx].u16_cnt		= 0u;
			                    g_st_ram1.run_auxiliary_data.sta_di_vote[u16_idx].u16_tim_old	= comm_tim_get_currt_cnt();
													g_st_ram2.run_auxiliary_data.sta_di_vote[u16_idx].u16_cnt		= 0u;
			                    g_st_ram2.run_auxiliary_data.sta_di_vote[u16_idx].u16_tim_old	= g_st_ram1.run_auxiliary_data.sta_di_vote[u16_idx].u16_tim_old;
		                    }
												else
													{
		                     	  u32_tmp_dat	= comm_tim_calc_diff(&g_st_ram1.run_auxiliary_data.sta_di_vote[u16_idx].u16_tim_old);
			/* Extern fault confirm --外部故障确认*/
														stp_sta_dat->u16_di &= U16_CLR_BIT[u16_idx]; 
			                    if(g_st_ram1.run_auxiliary_data.sta_di_vote[u16_idx].u16_cnt < CH_EX_ASYNC_FILT_TWOHAND)
										         {
				                        g_st_ram1.run_auxiliary_data.sta_di_vote[u16_idx].u16_cnt += (uint16_t)u32_tmp_dat;
												
		              	         }
									       else
										         {
				                       //stp_sta_dat->u16_di &= U16_CLR_BIT[u16_idx]; 
												       stp_sta_dat->u32_st_dev_sta |= U32_SET_BIT[u16_idx+CH_NUM_DI];  //zhou
			                       }
			/* Signal asynchronism confirm --信号异步确认*/
//		           	        if(CH_EX_ASYNC_FILT_TWOHAND < g_st_ram1.run_auxiliary_data.sta_di_vote[u16_idx].u16_cnt)
//											      {
//				                     // stp_sta_dat->u16_di &= U16_CLR_BIT[u16_idx]; 
//                              stp_sta_dat->u32_st_dev_sta |= U16_SET_BIT[u16_idx+6u];  												
//		            	          }
//							   	      else
//										       {
//				                     ;
//			                     }
													 g_st_ram2.run_auxiliary_data.sta_di_vote[u16_idx].u16_tim_old	= g_st_ram1.run_auxiliary_data.sta_di_vote[u16_idx].u16_tim_old;
                           g_st_ram2.run_auxiliary_data.sta_di_vote[u16_idx].u16_cnt = g_st_ram1.run_auxiliary_data.sta_di_vote[u16_idx].u16_cnt;													 
		                    }
									}break;
									case DI_TYPE_MAGNETIC_NO_BALANCE:  //磁性开关无通道平衡
									{
										if(((u16_mcua_di & U16_SET_BIT[u16_idx])!= 0u) && ((u16_mcub_di & U16_SET_BIT[u16_idx])== 0u))
										{
											if ((stp_sta_dat->u32_st_dev_sta&U32_SET_BIT[u16_idx+CH_NUM_DI])==0u) //zhou
											{
												stp_sta_dat->u16_di |= U16_SET_BIT[u16_idx];    //置输入为1
												stp_sta_dat->u32_st_dev_sta &= U32_CLR_BIT[u16_idx+CH_NUM_DI];   //zhou
											}
											else 
											{
												stp_sta_dat->u16_di &= U16_CLR_BIT[u16_idx];  //置输入为0
											}
										}
										else
										{
											stp_sta_dat->u16_di &= U16_CLR_BIT[u16_idx];    //置输入为0
										}
									}break;
		              default : break;
						}
	        }
	   }
	else
	  {
			//若有严重错误，则置位逻辑块输入单元为0，
			stp_sta_dat->u16_di=0x00u;
	  }
	stp_sta_dat_bak->u16_di	= stp_sta_dat->u16_di ;
	stp_sta_dat_bak->u32_st_dev_sta = stp_sta_dat->u32_st_dev_sta;
}


bool_t prg_logic_AandB(bool_t b8_ch1, bool_t b8_ch2)
{
	bool_t	b8_out	= BFALSE;
	/* CH1 & CH2 */
	if((b8_ch1 == BTRUE) && (b8_ch2 == BTRUE))
  {
		b8_out = BTRUE;
	}else{
		b8_out = BFALSE;
	}
	return b8_out;
}

bool_t prg_logic_AxnorB(bool_t b8_ch1, bool_t b8_ch2)
{
	bool_t	b8_out	= BFALSE;
	/* CH1 同或 CH2 */
	if(b8_ch1 == b8_ch2)
  {
		b8_out = BTRUE;
	}else{
		b8_out = BFALSE;
	}
	return b8_out;
}

bool_t prg_logic_AxorB(bool_t b8_ch1, bool_t b8_ch2)
{
	bool_t	b8_out	= BFALSE;
	/* CH1 异或 CH2 */
	if(b8_ch1 != b8_ch2)
  {
		b8_out = BTRUE;
	}else{
		b8_out = BFALSE;
	}
	return b8_out;
}

bool_t prg_logic_AorB(bool_t b8_ch1, bool_t b8_ch2)
{
	bool_t	b8_out	= BFALSE;
	/* CH1 || CH2 */
	if((b8_ch1 == BTRUE) || (b8_ch2 == BTRUE))
  {
		b8_out = BTRUE;
	}else{
		b8_out = BFALSE;
	}
	return b8_out;
}

bool_t prg_handle_two_hand( bool_t b8_control_out, dev_input_res_t *stp_ch1,\
							 dev_input_res_t *stp_ch2,\
							dev_input_res_t *stp_ch1_bak,\
							dev_input_res_t *stp_ch2_bak)
{
	uint32_t	u32_tmp_dat	= 0u;
	bool_t	b8_out	= BFALSE;
	if (b8_control_out==BTRUE)
	{
	   if((stp_ch1->b8_vld_sta == BTRUE)&&(stp_ch2->b8_vld_sta == BTRUE))
		 {
			 b8_out = BTRUE;
			 stp_ch1->b8_thld_sta =BTRUE;
			 stp_ch2->b8_thld_sta =BTRUE;
		 }
		 else
		 {
			 b8_out = BFALSE;
		 }
	}
	else
	{
	   if((stp_ch1->b8_vld_sta == BFALSE)&&(stp_ch2->b8_vld_sta == BFALSE))
		{ //若当前值都为0，输出为0，采样计数为0,置老值为0
	     stp_ch1->u16_cnt = 0u;
			 stp_ch1->u16_tim_old	= comm_tim_get_currt_cnt();		
			 b8_out	= BFALSE;	
			 stp_ch1->b8_thld_sta =BFALSE;
			 stp_ch2->b8_thld_sta =BFALSE;
	   }
		else if((stp_ch1->b8_vld_sta== BTRUE)&&(stp_ch1->b8_thld_sta== BFALSE)&&(stp_ch2->b8_vld_sta== BTRUE)&&(stp_ch2->b8_thld_sta== BFALSE))
	  {
			//u32_tmp_dat	= comm_tim_calc_diff(&stp_ch1->u16_tim_old);
			//stp_ch1->u16_cnt += (uint16_t)u32_tmp_dat;
			if (stp_ch1-> u16_cnt < TWO_HAND_LAG_TIME)
			{
					b8_out = BTRUE;
			}
			else
			{
			   b8_out=BFALSE;
			}
	  }
		else if((stp_ch1->b8_vld_sta== BTRUE)&&(stp_ch1->b8_thld_sta== BFALSE)&&(stp_ch2->b8_vld_sta== BFALSE)&&(stp_ch2->b8_thld_sta== BFALSE))
	  {
		  u32_tmp_dat	= comm_tim_calc_diff(&stp_ch1->u16_tim_old);
			stp_ch1->u16_cnt += (uint16_t)u32_tmp_dat;
		  b8_out=BFALSE;
	  }
   else if((stp_ch1->b8_vld_sta== BFALSE)&&(stp_ch1->b8_thld_sta== BFALSE)&&(stp_ch2->b8_vld_sta== BTRUE)&&(stp_ch2->b8_thld_sta== BFALSE))
	  {
		  u32_tmp_dat	= comm_tim_calc_diff(&stp_ch1->u16_tim_old);
			stp_ch1->u16_cnt += (uint16_t)u32_tmp_dat;
		  b8_out=BFALSE;
	  }
	 else
	  {
		b8_out=BFALSE;
	  }
	}
	stp_ch1_bak->b8_thld_sta = stp_ch1->b8_thld_sta;
	stp_ch1_bak->b8_vld_sta = stp_ch1->b8_vld_sta;
	stp_ch1_bak->u16_cnt = stp_ch1->u16_cnt;
	stp_ch1_bak->u16_tim_old = stp_ch1->u16_tim_old;
  stp_ch2_bak->b8_thld_sta = stp_ch2->b8_thld_sta;
	stp_ch2_bak->b8_vld_sta = stp_ch2->b8_vld_sta;
	stp_ch2_bak->u16_cnt = stp_ch2->u16_cnt;
	stp_ch2_bak->u16_tim_old = stp_ch2->u16_tim_old;
	return b8_out;
}

bool_t prg_handle_rstltoh( bool_t b8_in, bool_t b8_control_out, dev_input_res_t *stp_rst, dev_input_res_t *stp_rst_bak)
{
	//上升沿复位
	bool_t     b8_out = BFALSE;
	uint32_t   u32_tmp_dat = 0u;
	uint32_t   u32_tmp_dat1 = 0u;
	
	//排除程序第一次运行u16_di值为0,导致复位按钮上一次值发生变化;若u16_di采样值不为0后,进入到复位按钮上一次值设定语句
//	if(g_st_ram1.run_display_data.instrument_hmi.u16_di != 0u)
//	{
		//排除（1）当上升沿复位输出逻辑块在下降沿复位输出逻辑块之前（就是逻辑块下角标的排序）;（2）当上升沿复位输出逻辑块的输入为OFF时;
		// 下降沿复位输出逻辑块不会起作用：即输入为ON，复位按钮从1→0时，输出不会启动的问题
		if (b8_in == BTRUE)
		{
			//如果复位逻辑块输出为OFF
			if(b8_control_out== BFALSE)
			{
				//如果复位按钮当前值为低电平
				if(stp_rst->b8_vld_sta == BFALSE)
				{
					//如果复位按钮上一次值为高电平,保持300ms设置为BTRUE,超过300ms设置为BFALSE
					if(stp_rst->b8_thld_sta == BTRUE)
					{
						//延时300ms,为了复位按钮上一次的值不被更新，多线程应用，延时的同时mcu其它程序再跑
						u32_tmp_dat  = comm_tim_calc_diff(&stp_rst->u16_tim_old);
						stp_rst->u16_cnt += (uint16_t)u32_tmp_dat;
						if (stp_rst-> u16_cnt < RST_LAG_TIME)
						{
							stp_rst->b8_thld_sta=BTRUE;
						}
						else
						{
							stp_rst->b8_thld_sta = BFALSE;
							stp_rst->u16_cnt = 0u;
							stp_rst->u16_tim_old  = comm_tim_get_currt_cnt();                
						}
					}
					//如果复位按钮上一次值为低电平，上次值还置为BFALSE
					else if(stp_rst->b8_thld_sta == BFALSE)
					{
						stp_rst->b8_thld_sta = BFALSE;
						stp_rst->u16_cnt = 0u;
						stp_rst->u16_tim_old  = comm_tim_get_currt_cnt(); 
					}
					//如果复位按钮上一次值为0,则设置为BFALSE
					else
					{
						stp_rst->b8_thld_sta = BFALSE;
						stp_rst->u16_cnt = 0u;
						stp_rst->u16_tim_old  = comm_tim_get_currt_cnt(); 
					}
				}
				//如果复位按钮当前值为高电平
				else
				{	
					//如果复位按钮上一次值为低电平,保持300ms设置为BFALSE,超过300ms设置为BTRUE
					if(stp_rst->b8_thld_sta == BFALSE)
					{
						u32_tmp_dat1  = comm_tim_calc_diff(&stp_rst->u16_tim_old);
						stp_rst->u16_cnt += (uint16_t)u32_tmp_dat1;
						//保持300ms
						if (stp_rst-> u16_cnt < RST_LAG_TIME)
						{
							stp_rst->b8_thld_sta = BFALSE;
						}
						//否则上次值更新为高电平
						else
						{
								stp_rst->b8_thld_sta = BTRUE;
							stp_rst->u16_cnt = 0u;
							stp_rst->u16_tim_old  = comm_tim_get_currt_cnt();  
						}
					}
					//如果复位按钮上一次值为高电平，上次值还置为BTRUE
					else if(stp_rst->b8_thld_sta == BTRUE)
					{
						stp_rst->b8_thld_sta = BTRUE;
						stp_rst->u16_cnt = 0u;
						stp_rst->u16_tim_old  = comm_tim_get_currt_cnt();       
					}
					//如果复位按钮上一次值为0,则设置为BTRUE
					else
					{
						stp_rst->b8_thld_sta = BTRUE;
						stp_rst->u16_cnt = 0u;
						stp_rst->u16_tim_old  = comm_tim_get_currt_cnt();     
					}		
				}
			}
			else
			{
				;
			}
		}
		else
		{
			;
		}
//	}
//	else 
//	{
//		;
//	}
	
	if ((b8_in== BTRUE)&&(b8_control_out== BTRUE))
	{
		b8_out = BTRUE; //若复位模块的输入和输出都为高电平，保持当前高电平输出
	}
	else if((b8_in== BTRUE)&&(b8_control_out== BFALSE))
	{ //若输入为高电平，输出为低电平，则考虑复位
		if (stp_rst->b8_vld_sta == BTRUE)
	  {
			if (stp_rst->b8_thld_sta== BFALSE)
			{
				b8_out=BTRUE; 
			}
			else
			{
				b8_out=BFALSE;
			}
	  }
	  else
	  { 
			  ;
	  }
	}
	else
	{ //其他情况均为0
		b8_out=BFALSE;
	}
	stp_rst_bak->b8_thld_sta = stp_rst->b8_thld_sta;
	stp_rst_bak->b8_vld_sta = stp_rst->b8_vld_sta;
	stp_rst_bak->u16_cnt = stp_rst->u16_cnt;
	stp_rst_bak->u16_tim_old = stp_rst->u16_tim_old;
	return b8_out;
}
bool_t prg_handle_rsthtol( bool_t b8_in, bool_t b8_control_out, dev_input_res_t *stp_rst, dev_input_res_t *stp_rst_bak)
{
	//下降沿复位
	bool_t     b8_out = BFALSE;
	uint32_t   u32_tmp_dat = 0u;
	uint32_t   u32_tmp_dat1 = 0u;
	
	//排除程序第一次运行u16_di值为0,导致复位按钮上一次值发生变化;若u16_di采样值不为0后,进入到复位按钮上一次值设定语句
//	if(g_st_ram1.run_display_data.instrument_hmi.u16_di != 0u)
//	{
		//排除（1）当上升沿复位输出逻辑块在下降沿复位输出逻辑块之前（就是逻辑块下角标的排序）;（2）当上升沿复位输出逻辑块的输入为OFF时;
		// 下降沿复位输出逻辑块不会起作用：即输入为ON，复位按钮从1→0时，输出不会启动的问题
		if (b8_in == BTRUE)
		{
			//如果复位逻辑块输出为OFF
			if(b8_control_out== BFALSE)
			{
				//如果复位按钮当前值为低电平
				if(stp_rst->b8_vld_sta == BFALSE)
				{
					//如果复位按钮上一次值为高电平
					if (stp_rst->b8_thld_sta == BTRUE)
					{
						u32_tmp_dat  = comm_tim_calc_diff(&stp_rst->u16_tim_old);
						stp_rst->u16_cnt += (uint16_t)u32_tmp_dat;
						if (stp_rst-> u16_cnt < RST_LAG_TIME)
						{
							stp_rst->b8_thld_sta = BTRUE;
						}
						else
						{
							stp_rst->b8_thld_sta = BFALSE;
							stp_rst->u16_cnt = 0u;
							stp_rst->u16_tim_old  = comm_tim_get_currt_cnt();                
						}
					}
					//如果复位按钮上一次值为低电平，上次值还置为BFALSE
					else if(stp_rst->b8_thld_sta == BFALSE)
					{
						stp_rst->b8_thld_sta = BFALSE;
						stp_rst->u16_cnt = 0u;
						stp_rst->u16_tim_old  = comm_tim_get_currt_cnt(); 
					}
					//如果复位按钮上一次值为0,则设置为BFALSE
					else
					{
						stp_rst->b8_thld_sta = BFALSE;
						stp_rst->u16_cnt = 0u;
						stp_rst->u16_tim_old  = comm_tim_get_currt_cnt(); 
					}
				}
				//如果复位按钮当前值为高电平
				else
				{
					//如果复位按钮上一次值为低电平,保持300ms设置为BFALSE,超过300ms设置为BTRUE
					if(stp_rst->b8_thld_sta == BFALSE)
					{
						u32_tmp_dat1  = comm_tim_calc_diff(&stp_rst->u16_tim_old);
						stp_rst->u16_cnt += (uint16_t)u32_tmp_dat1;
						//保持300ms
						if (stp_rst-> u16_cnt < RST_LAG_TIME)
						{
							stp_rst->b8_thld_sta = BFALSE;
						}
						//否则上次值更新为高电平
						else
						{
								stp_rst->b8_thld_sta = BTRUE;
							stp_rst->u16_cnt = 0u;
							stp_rst->u16_tim_old  = comm_tim_get_currt_cnt();  
						}
					}
					//如果复位按钮上一次值为高电平，上次值还置为BTRUE
					else if(stp_rst->b8_thld_sta == BTRUE)
					{
						stp_rst->b8_thld_sta = BTRUE;
						stp_rst->u16_cnt = 0u;
						stp_rst->u16_tim_old  = comm_tim_get_currt_cnt();       
					}
					//如果复位按钮上一次值为0,则设置为BTRUE
					else
					{
						stp_rst->b8_thld_sta = BTRUE;
						stp_rst->u16_cnt = 0u;
						stp_rst->u16_tim_old  = comm_tim_get_currt_cnt();     
					}		
				}
			}
			else
			{
				;
			}
		}
		else
		{
			;
		}
//	}
//	else
//	{
//		;
//	}
		
	
  if ((b8_in== BTRUE)&&(b8_control_out== BTRUE))
	{
		b8_out = BTRUE;		 //若复位模块的输入和输出都为高电平，保持当前高电平输出
	}
	else if((b8_in== BTRUE)&&(b8_control_out== BFALSE))
	{ 
		//若输入为高电平，输出为低电平，则考虑复位
		if (stp_rst->b8_vld_sta == BFALSE)//若复位按钮当前值为低电平
		{
			if (stp_rst->b8_thld_sta== BTRUE)//复位按钮上次值为高电平，则输出为高
			 {
				  b8_out=BTRUE;
			 }
			 else
			 {
				  b8_out=BFALSE;
			 } 
		}
		else
		{ 
			;
		}
	}
	else
	{
		b8_out=BFALSE;
	}
	stp_rst_bak->b8_thld_sta = stp_rst->b8_thld_sta;
	stp_rst_bak->b8_vld_sta = stp_rst->b8_vld_sta;
	stp_rst_bak->u16_cnt = stp_rst->u16_cnt;
	stp_rst_bak->u16_tim_old = stp_rst->u16_tim_old;
	return b8_out;
}

bool_t prg_handle_rsthtoh( bool_t b8_in, bool_t b8_control_out, const dev_input_res_t *stp_rst, dev_input_res_t *stp_rst_bak)
{
	bool_t	b8_out	= BFALSE;
  if ((b8_in== BTRUE)&&(b8_control_out== BTRUE))
	 {
		 b8_out = BTRUE; //若复位模块的输入和输出都为高电平，保持当前高电平输出
	 }
	else if((b8_in== BTRUE)&&(b8_control_out== BFALSE))
	{ //若输入为高电平，输出为低电平，则考虑复位
	   if (stp_rst->b8_vld_sta == BTRUE)
		 {
	     b8_out = BTRUE;
		 }
		 else
		 { 
			 b8_out = BFALSE;
		 }
	}
	else
		{ //其他情况均为0
		 b8_out=BFALSE;
	  }
	stp_rst_bak->b8_thld_sta = stp_rst->b8_thld_sta;
	stp_rst_bak->b8_vld_sta = stp_rst->b8_vld_sta;
	stp_rst_bak->u16_cnt = stp_rst->u16_cnt;
	stp_rst_bak->u16_tim_old = stp_rst->u16_tim_old;
	return b8_out;
}

bool_t prg_handle_delay_on( bool_t b8_in, bool_t b8_control_out, uint32_t u32_tim_prm, layer_output_res_t *stp_delay, layer_output_res_t *stp_delay_bak)
{
	uint32_t	u32_tmp_dat	= 0u;
	bool_t	b8_out	= BFALSE;
	if (( b8_in==BTRUE)&&(b8_control_out==BFALSE))
	{
 		
		if(stp_delay->u32_cnt< u32_tim_prm)   //当前累计计数值
		{
			u32_tmp_dat	= comm_tim_calc_diff(&stp_delay->u16_tim_old); //取得当前间隔值
		  stp_delay->u32_cnt += u32_tmp_dat; //	
      b8_out	= BFALSE;			
		}
		else
		{
		 b8_out=BTRUE;
		  stp_delay->u16_tim_old	= comm_tim_get_currt_cnt();  //获取当前计数值
		}
	}
	else if (( b8_in==BTRUE)&&(b8_control_out==BTRUE))
	{
		b8_out=BTRUE;
		stp_delay->u16_tim_old	= comm_tim_get_currt_cnt();
	}
	else
	{
		b8_out=BFALSE;
		stp_delay->u32_cnt=0u;
		stp_delay->u16_tim_old	= comm_tim_get_currt_cnt();
	}
	
	stp_delay_bak->u32_cnt=stp_delay->u32_cnt;
	stp_delay_bak->u16_tim_old=stp_delay->u16_tim_old;
	return b8_out;
}

bool_t prg_handle_delay_off( bool_t b8_in, bool_t b8_control_out,uint32_t u32_tim_prm, layer_output_res_t *stp_delay, layer_output_res_t *stp_delay_bak)
{
	uint32_t	u32_tmp_dat	= 0u;
	bool_t	b8_out	= BFALSE;
	if (( b8_in==BFALSE)&&(b8_control_out==BTRUE))
	{
		if(stp_delay->u32_cnt< u32_tim_prm)
		{
			u32_tmp_dat	= comm_tim_calc_diff(&stp_delay->u16_tim_old); //取得当前间隔值
		  stp_delay->u32_cnt += u32_tmp_dat; //		
			b8_out =BTRUE;
		}
		else
		{
		 b8_out=BFALSE;
		 stp_delay->u16_tim_old	= comm_tim_get_currt_cnt();
		}
	}
	else if (( b8_in==BFALSE)&&(b8_control_out==BFALSE)) //若当前输入输出均为OFF,则输出保持OFF,安全考虑
	{
	   b8_out=BFALSE;
		 stp_delay->u16_tim_old	= comm_tim_get_currt_cnt();
	}
	else //若输入为ON,则立即输出高电平
	{
		b8_out=BTRUE;
		stp_delay->u32_cnt=0u;
		stp_delay->u16_tim_old	= comm_tim_get_currt_cnt();
	}
	
	stp_delay_bak->u32_cnt=stp_delay->u32_cnt;
	stp_delay_bak->u16_tim_old=stp_delay->u16_tim_old;
	return b8_out;
}

/***************************************************************心跳处理**************************************************/
bool_t  prg_heart_beat_monitor(bool_t b8_in,  uint32_t u32_heart_tim, layer_output_res_t *hbs_delay, layer_output_res_t *hbs_delay_bak)
{
	uint32_t	u32_tmp_dat	= 0u;
	//bool_t	b8_out	= BFALSE;
	//hbs_delay->b8_res[1] = b8_out;
  //hbs_delay->b8_resa[0]  = b8_in;   //当前值存入
	//hbs_delay->b8_old_data = hbs_delay->b8_old_data;
	// hbs_delay->u32_cnt=0;
	
	if((hbs_delay->u32_cnt < (u32_heart_tim))&&(  b8_in == hbs_delay->b8_res[0]))   //当前电平 = 上一次电平   输出保持
		{				
			 u32_tmp_dat = comm_tim_calc_diff(&hbs_delay->u16_tim_old); //取得当前间隔值
			 hbs_delay->u32_cnt  += u32_tmp_dat;					 
			}	
			if((hbs_delay->u32_cnt < u32_heart_tim)&&( b8_in != hbs_delay->b8_res[0]))   //当前电平 ≠ 上一次电平   输出为1清零计数
			{
			 hbs_delay->b8_res[1] = BTRUE;
       hbs_delay->u32_cnt =0u;				
			 hbs_delay->u16_tim_old  = comm_tim_get_currt_cnt();
			 	
			}				
			if((hbs_delay->u32_cnt >= u32_heart_tim)&&( b8_in == hbs_delay->b8_res[0]))     //输出为0
	{				
		 hbs_delay->b8_res[1]  =BFALSE;
		 //hbs_delay->u16_tim_old  = comm_tim_get_currt_cnt();
	}
	if((hbs_delay->u32_cnt >= u32_heart_tim)&&( b8_in != hbs_delay->b8_res[0])) 
	{				
		hbs_delay->u32_cnt =0u; 
		hbs_delay->u16_tim_old  = comm_tim_get_currt_cnt();	
	}
		
/*****************************************************/		
//	}	
//	else 
//	{
//		 b8_out=BFALSE;
//	   hbs_delay->u16_tim_old  = comm_tim_get_currt_cnt();					
//	}
  hbs_delay->b8_res[0]  = b8_in;
	hbs_delay_bak->u32_cnt  = hbs_delay->u32_cnt ;
	hbs_delay_bak->u16_tim_old = hbs_delay->u16_tim_old ;
	hbs_delay_bak->b8_res[0]  = hbs_delay->b8_res[0] ;         //b8_res[0]: 上一次值  b8_res[1]:当前值
	hbs_delay_bak->b8_res[1] = hbs_delay->b8_res[1] ;
	
//  hbs_delay->b8_old_data  = b8_out;

	return hbs_delay->b8_res[1];
  }

/************************************************频率输出处理（0413）**************************************************/
bool_t  prg_fre_out(bool_t b8_in,  uint32_t u32_fre_tim, layer_output_res_t *fre_delay, layer_output_res_t *fre_delay_bak)
{
	uint32_t	u32_tmp_cont	= 0u;
//	bool_t	b8_out	= B8_fre_first_out;
//	fre_delay->b8_old_data = B8_fre_first_out;

  bool_t	b8_out	= fre_delay->b8_res[0];  //b8_now_data
	
	if(b8_in==BTRUE ) //频率输出
	{	
		if(fre_delay->b8_res[0]	== BFALSE)
		{
			if(fre_delay->u32_cnt < u32_fre_tim )  //判断频率值
			{
				u32_tmp_cont =  comm_tim_calc_diff(&fre_delay->u16_tim_old); //取得当前间隔值
				fre_delay->u32_cnt += u32_tmp_cont;
				fre_delay->b8_res[1] = BFALSE ;
				//fre_delay->b8_res[0] = BFALSE;
			}	
			else 
			{
			b8_out = BTRUE ;
			fre_delay->b8_res[1]= BTRUE;
			fre_delay->u32_cnt =0U;
	 //   fre_delay->u16_tim_old  = comm_tim_get_currt_cnt();			
			}
		}
		else 
		{
			if(fre_delay->u32_cnt < u32_fre_tim )
			{
				u32_tmp_cont =  comm_tim_calc_diff(&fre_delay->u16_tim_old); //取得当前间隔值
				fre_delay->u32_cnt += u32_tmp_cont;
				b8_out = BTRUE ;
				fre_delay->b8_res[1]= BTRUE;
			}	
			else 
			{
			b8_out = BFALSE ;
			fre_delay->b8_res[1]= BFALSE;
			fre_delay->u32_cnt =0U;	
	//		fre_delay->u16_tim_old  = comm_tim_get_currt_cnt();
			}
		}
  }
	else if(b8_in == BFALSE ) 
	{
	fre_delay->b8_res[1]= BFALSE;
//	 b8_out = BFALSE ;
//		soft_dly_us(10);
	// fre_delay->u32_cnt =0U;
	 fre_delay->u16_tim_old  = comm_tim_get_currt_cnt();
	}
	fre_delay->b8_res[0]  = fre_delay->b8_res[1];
// fre_delay->b8_old_data = 	b8_out;	
	fre_delay_bak->u32_cnt  = fre_delay->u32_cnt ;
	fre_delay_bak->u16_tim_old = fre_delay->u16_tim_old ;
	fre_delay_bak->b8_res[0] = fre_delay->b8_res[0] ;
	fre_delay_bak->b8_res[1] = fre_delay->b8_res[1];
	//B8_fre_first_out = b8_out;
	

	
	return fre_delay->b8_res[1];
}



bool_t prg_logic_AandBandC(bool_t b8_ch1, bool_t b8_ch2,bool_t b8_ch3)
{
	bool_t	b8_out	= BFALSE;
	/* CH1 & CH2 */
	if((b8_ch1 == BTRUE) && (b8_ch2 == BTRUE)&&(b8_ch3 == BTRUE))
  {
		b8_out = BTRUE;
	}else{
		b8_out = BFALSE;
	}
	return b8_out;
}

bool_t prg_logic_AorBorC(bool_t b8_ch1, bool_t b8_ch2,bool_t b8_ch3)
{
	bool_t	b8_out	= BFALSE;
	if((b8_ch1 == BTRUE) || (b8_ch2 == BTRUE)||(b8_ch3 == BTRUE))
  {
		b8_out = BTRUE;
	}else{
		b8_out = BFALSE;
	}
	return b8_out;
}

bool_t prg_logic_notA(bool_t b8_ch1)
{
	bool_t	b8_out	= BFALSE;
	if(b8_ch1 == BTRUE)
  {
		b8_out = BFALSE;
	}else{
		b8_out = BTRUE;
	}
	return b8_out;
}


static void prg_di_filt(bool_t b8_curt_sta, di_filt_dat_t *stp_sta_dat, di_filt_dat_t *stp_sta_dat_bak)
{
	uint32_t	u32_tmp_dat	= 0u;
	if(b8_curt_sta == stp_sta_dat->b8_thld_sta)//若当前值和上次的采样值一致，则进行滤波判断
	{
		if(stp_sta_dat->u16_filt_cnt < DI_FILT_MAX)
		{
			u32_tmp_dat	= comm_tim_calc_diff(&stp_sta_dat->u16_tim_old);//获取和上一次的间隔值
			stp_sta_dat->u16_filt_cnt += (uint16_t)u32_tmp_dat;//加上上一次的间隔时间
		}
		else
		{
			;
		}
	}
	else//若当前值和上次的采样值不一样
	{
		stp_sta_dat->u16_tim_old	= comm_tim_get_currt_cnt();//获得本次时间点
		stp_sta_dat-> u16_filt_cnt	= 0u;//从新开始计时
	}
	
	stp_sta_dat->b8_thld_sta = b8_curt_sta;//把当前值给到需要进行比较的值
	
	if(stp_sta_dat->u16_filt_cnt >= stp_sta_dat->u16_filt_pram)//若维持10ms,则认为信号为正确采样了
	{
		stp_sta_dat-> b8_vld_sta = stp_sta_dat->b8_thld_sta;//将比较值给到有效值单元
	}
	else
	{
		;
	}
	
	stp_sta_dat_bak->u16_filt_cnt	= stp_sta_dat->u16_filt_cnt	;
	stp_sta_dat_bak->u16_tim_old	= stp_sta_dat->u16_tim_old	;
	stp_sta_dat_bak->b8_thld_sta	= stp_sta_dat->b8_thld_sta	;
	stp_sta_dat_bak->b8_vld_sta		= stp_sta_dat->b8_vld_sta	;
}

void prg_write_flash(void)   //写FLASH操作
{
	uint8_t		u8_err_sta	= 0u;
	//uint16_t	u16_tmp		= 0u;
	uint16_t	u16_crc_val	= 0u;
	uint16_t    u16_tmp1 = 0u;
	uint16_t    u16_tmp2 = 0u;
	u16_tmp1 = sizeof(config_pra_t);  //取得组态数据的长度
	u16_tmp2 = sizeof(config_project_name_t);  //取得组态数据的长度
	u16_crc_val = CodeCRCCal((uint8_t*)&g_st_ram1, u16_tmp1-4u);//计算出CRC校验  6->4
	if(u16_crc_val == g_st_ram1.config_parameter.u16_config_crc)//若CRC校验正确
	{
	  prg_copy_ram_BtoA((uint8_t *)&g_u16_flash_dat,(uint8_t *)&g_st_ram1,u16_tmp1+u16_tmp2); //讲RAM中组态数据赋值给FLSH数组
		u8_err_sta = write_prg_dat(); //写FLASH
	}
	else
	{
	  u8_err_sta = 2u;
	}
	if(u8_err_sta != 0u)
	{
		g_st_ram1.run_display_data.instrument_hmi.u8_dev_fault |= BF_DIAG1_PRG; //置FLSAH故障
		g_st_ram2.run_display_data.instrument_hmi.u8_dev_fault |= BF_DIAG1_PRG; //置FLSAH故障
	}
	else
	{
		g_st_ram1.run_display_data.instrument_hmi.u8_dev_fault &= ~BF_DIAG1_PRG; //清FLSAH故障
		g_st_ram2.run_display_data.instrument_hmi.u8_dev_fault &= ~BF_DIAG1_PRG; //清FLSAH故障
		//led_set_red(BFALSE);
		//g_st_ram1.run_display_data.instrument_hmi.u16_led &= (uint16_t)~RED_LED_LIGHT; //红灯灭 指示
		//g_st_ram2.run_display_data.instrument_hmi.u16_led &= (uint16_t)~RED_LED_LIGHT; //红灯灭 指示
	}
}

static void prg_relay_out_adj(bool_t b8_curt_val, uint8_t u8_mcu_addr,\
							uint8_t u8_ch_no, 
							relay_sta_dat_t *stp_ry_dat,\
							relay_sta_dat_t *stp_dat_bak)
{
	uint32_t	u32_tmp_dat		= 0u;
	uint16_t	u16_out_dat		= 0u;
	uint8_t		u8_shift_idx	= 0u;
	if(b8_curt_val != stp_ry_dat->b8_out_old){
		if(stp_ry_dat->st_tim.u16_cnt < RELAY_ADJ_TIME){
			u32_tmp_dat	= comm_tim_calc_diff(&stp_ry_dat->st_tim.u16_tim_old);
			stp_ry_dat->st_tim.u16_cnt += u32_tmp_dat;
		}else{
			;
		}
	}else{
		stp_ry_dat->st_tim.u16_tim_old	= comm_tim_get_currt_cnt();
		stp_ry_dat->st_tim.u16_cnt		= 0u;
	}
	stp_dat_bak->st_tim.u16_cnt		= stp_ry_dat->st_tim.u16_cnt;
	stp_dat_bak->st_tim.u16_tim_old	= stp_ry_dat->st_tim.u16_tim_old;

	if(ADDR_MCU_A == u8_mcu_addr){
		u8_shift_idx	= 0u;
	}else{
		u8_shift_idx	= 2u;
	}

	if(b8_curt_val == BTRUE){
		u16_out_dat = 0x000Fu;
		if(stp_ry_dat->st_tim.u16_cnt < RELAY_ADJ_TIME){
			u16_out_dat &= (uint16_t)U16_CLR_BIT[stp_ry_dat->u16_flip_cnt];
		}else{
			stp_ry_dat->b8_out_old	= BTRUE;
		}
		if(stp_ry_dat->b8_out_old == BTRUE){
			u16_out_dat = 0x000Fu;
		}else{
			;
		}
	}else if(stp_ry_dat->b8_out_old == BTRUE){
		u16_out_dat = 0x000Fu;
		if(stp_ry_dat->st_tim.u16_cnt < RELAY_ADJ_TIME){
			u16_out_dat &= (uint16_t)U16_CLR_BIT[stp_ry_dat->u16_flip_cnt];
		}else{
			u16_out_dat = 0x0000u;
			stp_ry_dat->b8_out_old	= BFALSE;
			if(stp_ry_dat->u16_flip_cnt < 3u){
				stp_ry_dat->u16_flip_cnt ++;
			}else{
				stp_ry_dat->u16_flip_cnt = 0u;
			}
		}
	}else{
		u16_out_dat = 0x0000u;
	}
	stp_ry_dat->u16_adj_rslt = (uint16_t)(u16_out_dat >> u8_shift_idx) & BF_DO_VAL_RO;

	stp_dat_bak->b8_out_old		= stp_ry_dat->b8_out_old;
	stp_dat_bak->u16_flip_cnt	= stp_ry_dat->u16_flip_cnt;
	stp_dat_bak->u16_adj_rslt	= stp_ry_dat->u16_adj_rslt;
}

void prg_read_flash(void)  
{
	uint8_t   u8_count = 0;
	uint16_t	u16_crc_val	= 0u;
	uint16_t    u16_tmp1 = 0u;
	uint16_t   u16_tmp2=0u;
	uint16_t   u16_tmp3=0u;
	u16_tmp1 = sizeof(config_pra_t);   //取得组态数据的长度
	u16_tmp3 = sizeof(config_project_name_t);  //取得组态数据的长度
	read_prg_dat();//读FLASH存储数据
	u16_crc_val = CodeCRCCal((uint8_t*)g_u16_flash_dat, u16_tmp1-4u);   //计算出组态数据CRC校验
	u16_tmp2 =(88u+(12u*NUM_LOGIC))>>1u;  //取值1半  u16 --> u8
	if(u16_crc_val != g_u16_flash_dat[u16_tmp2])//若CRC校验不对，则对现有RAM数据进行初始化
	{
     /*前面RAM1/RAM2已初始化完成均为0u，故仅对工厂组态数据进行初始化   */
     g_st_ram1.config_parameter.factory_config_pra.u16_password =  8901u; //初始化密码
     g_st_ram2.config_parameter.factory_config_pra.u16_password =  8901u; //初始化密码
		
		
		 g_st_ram1.config_parameter.factory_config_pra.u8_type =  6u; //产品型号 CZSR8903
		 g_st_ram2.config_parameter.factory_config_pra.u8_type =  6u; //产品型号 CZSR8903
		
		 g_st_ram1.config_parameter.factory_config_pra.u8_config_id =  255u; //8.0代码
		 g_st_ram2.config_parameter.factory_config_pra.u8_config_id =  255u; //8.0代码
		
		 g_st_ram1.config_parameter.factory_config_pra.u8_pulse_detection_gap = 2u;   //输入脉冲测试时间
		 g_st_ram2.config_parameter.factory_config_pra.u8_pulse_detection_gap = 2u;   //输入脉冲测试时间
		
//		 g_st_ram1.config_parameter.u8_sensor_type[0]=  1u; //CH1传感器 zhou
//		 g_st_ram2.config_parameter.u8_sensor_type[0]=  1u; //CH1传感器
		
//		 for(u8_count=0; u8_count<3; u8_count++)
//		{
//		 g_st_ram1.config_parameter.input_sensor_control[u8_count].u8_sensor_type =1;  ////CH1~CH10传感器  双通道急停按钮
//		 g_st_ram2.config_parameter.input_sensor_control[u8_count].u8_sensor_type =1;
//		}
//		 g_st_ram1.config_parameter.input_sensor_control[9].u8_sensor_type =9;  ////CH1~CH10传感器  双通道急停按钮
//		 g_st_ram2.config_parameter.input_sensor_control[9].u8_sensor_type =9;

     /* 输入传感器缺省值 */ 
     g_st_ram1.config_parameter.input_sensor_control[0].u8_sensor_type =1; 		
		 g_st_ram1.config_parameter.input_sensor_control[0].u8_sensor_add =0u;
		 g_st_ram1.config_parameter.input_sensor_control[0].u16_input_x = 128u;
		 g_st_ram1.config_parameter.input_sensor_control[0].u16_input_y = 116u;
		 
		 g_st_ram2.config_parameter.input_sensor_control[0].u8_sensor_type =1; 		
		 g_st_ram2.config_parameter.input_sensor_control[0].u8_sensor_add =0u;
		 g_st_ram2.config_parameter.input_sensor_control[0].u16_input_x = 128u;
		 g_st_ram2.config_parameter.input_sensor_control[0].u16_input_y = 116u;
		 
		 g_st_ram1.config_parameter.input_sensor_control[1].u8_sensor_type =1; 		
		 g_st_ram1.config_parameter.input_sensor_control[1].u8_sensor_add =0u;
		 g_st_ram1.config_parameter.input_sensor_control[1].u16_input_x = 128u;
		 g_st_ram1.config_parameter.input_sensor_control[1].u16_input_y = 206u;
		 
		 g_st_ram2.config_parameter.input_sensor_control[1].u8_sensor_type =1; 		
		 g_st_ram2.config_parameter.input_sensor_control[1].u8_sensor_add =0u;
		 g_st_ram2.config_parameter.input_sensor_control[1].u16_input_x = 128u;
		 g_st_ram2.config_parameter.input_sensor_control[1].u16_input_y = 206u;
		 
		 g_st_ram1.config_parameter.input_sensor_control[2].u8_sensor_type =1; 		
		 g_st_ram1.config_parameter.input_sensor_control[2].u8_sensor_add =0u;
		 g_st_ram1.config_parameter.input_sensor_control[2].u16_input_x = 128u;
		 g_st_ram1.config_parameter.input_sensor_control[2].u16_input_y = 296u;
		 
		 g_st_ram2.config_parameter.input_sensor_control[2].u8_sensor_type =1; 		
		 g_st_ram2.config_parameter.input_sensor_control[2].u8_sensor_add =0u;
		 g_st_ram2.config_parameter.input_sensor_control[2].u16_input_x = 128u;
		 g_st_ram2.config_parameter.input_sensor_control[2].u16_input_y = 296u;
		 
		 g_st_ram1.config_parameter.input_sensor_control[3].u8_sensor_type =1; 		
		 g_st_ram1.config_parameter.input_sensor_control[3].u8_sensor_add =0u;
		 g_st_ram1.config_parameter.input_sensor_control[3].u16_input_x = 128u;
		 g_st_ram1.config_parameter.input_sensor_control[3].u16_input_y = 386u;
		 
		 g_st_ram2.config_parameter.input_sensor_control[3].u8_sensor_type =1; 		
		 g_st_ram2.config_parameter.input_sensor_control[3].u8_sensor_add =0u;
		 g_st_ram2.config_parameter.input_sensor_control[3].u16_input_x = 128u;
		 g_st_ram2.config_parameter.input_sensor_control[3].u16_input_y = 386u;
		 
		 g_st_ram1.config_parameter.input_sensor_control[4].u8_sensor_type =1; 		
		 g_st_ram1.config_parameter.input_sensor_control[4].u8_sensor_add =0u;
		 g_st_ram1.config_parameter.input_sensor_control[4].u16_input_x = 128u;
		 g_st_ram1.config_parameter.input_sensor_control[4].u16_input_y = 476u;
		 
		 g_st_ram2.config_parameter.input_sensor_control[4].u8_sensor_type =1; 		
		 g_st_ram2.config_parameter.input_sensor_control[4].u8_sensor_add =0u;
		 g_st_ram2.config_parameter.input_sensor_control[4].u16_input_x = 128u;
		 g_st_ram2.config_parameter.input_sensor_control[4].u16_input_y = 476u;
		 
		 g_st_ram1.config_parameter.input_sensor_control[5].u8_sensor_type =1; 		
		 g_st_ram1.config_parameter.input_sensor_control[5].u8_sensor_add =0u;
		 g_st_ram1.config_parameter.input_sensor_control[5].u16_input_x = 128u;
		 g_st_ram1.config_parameter.input_sensor_control[5].u16_input_y = 566u;
																										 
		 g_st_ram2.config_parameter.input_sensor_control[5].u8_sensor_type =1; 		
		 g_st_ram2.config_parameter.input_sensor_control[5].u8_sensor_add =0u;
		 g_st_ram2.config_parameter.input_sensor_control[5].u16_input_x = 128u;
		 g_st_ram2.config_parameter.input_sensor_control[5].u16_input_y = 566u;
		 
		 g_st_ram1.config_parameter.input_sensor_control[6].u8_sensor_type =1; 		
		 g_st_ram1.config_parameter.input_sensor_control[6].u8_sensor_add =0u;
		 g_st_ram1.config_parameter.input_sensor_control[6].u16_input_x = 128u;
		 g_st_ram1.config_parameter.input_sensor_control[6].u16_input_y = 656u;
																										 
		 g_st_ram2.config_parameter.input_sensor_control[6].u8_sensor_type =1; 		
		 g_st_ram2.config_parameter.input_sensor_control[6].u8_sensor_add =0u;
		 g_st_ram2.config_parameter.input_sensor_control[6].u16_input_x = 128u;
		 g_st_ram2.config_parameter.input_sensor_control[6].u16_input_y = 656u;
		 
		 g_st_ram1.config_parameter.input_sensor_control[7].u8_sensor_type =1; 		
		 g_st_ram1.config_parameter.input_sensor_control[7].u8_sensor_add =0u;
		 g_st_ram1.config_parameter.input_sensor_control[7].u16_input_x = 128u;
		 g_st_ram1.config_parameter.input_sensor_control[7].u16_input_y = 746u;
																										 
		 g_st_ram2.config_parameter.input_sensor_control[7].u8_sensor_type =1; 		
		 g_st_ram2.config_parameter.input_sensor_control[7].u8_sensor_add =0u;
		 g_st_ram2.config_parameter.input_sensor_control[7].u16_input_x = 128u;
		 g_st_ram2.config_parameter.input_sensor_control[7].u16_input_y = 746u;
		 
		 g_st_ram1.config_parameter.input_sensor_control[8].u8_sensor_type =1; 		
		 g_st_ram1.config_parameter.input_sensor_control[8].u8_sensor_add =0u;
		 g_st_ram1.config_parameter.input_sensor_control[8].u16_input_x = 128u;
		 g_st_ram1.config_parameter.input_sensor_control[8].u16_input_y = 836u;
																										 
		 g_st_ram2.config_parameter.input_sensor_control[8].u8_sensor_type =1; 		
		 g_st_ram2.config_parameter.input_sensor_control[8].u8_sensor_add =0u;
		 g_st_ram2.config_parameter.input_sensor_control[8].u16_input_x = 128u;
		 g_st_ram2.config_parameter.input_sensor_control[8].u16_input_y = 836u;
		 
		 g_st_ram1.config_parameter.input_sensor_control[9].u8_sensor_type =1; 		
		 g_st_ram1.config_parameter.input_sensor_control[9].u8_sensor_add =0u;
		 g_st_ram1.config_parameter.input_sensor_control[9].u16_input_x = 128u;
		 g_st_ram1.config_parameter.input_sensor_control[9].u16_input_y = 936u;
																										 
		 g_st_ram2.config_parameter.input_sensor_control[9].u8_sensor_type =1; 		
		 g_st_ram2.config_parameter.input_sensor_control[9].u8_sensor_add =0u;
		 g_st_ram2.config_parameter.input_sensor_control[9].u16_input_x = 128u;
		 g_st_ram2.config_parameter.input_sensor_control[9].u16_input_y = 936u;
		 
		 
		 /* 逻辑块缺省值 */ 
		 /* **********************1:A&B&c******************************** */
		 g_st_ram1.config_parameter.logic_control[0].u8_function_type=102u;   //66 A&B&c
		 g_st_ram1.config_parameter.logic_control[0].u8_function_in1_add=107u;  //67
		 g_st_ram1.config_parameter.logic_control[0].u8_function_in2_add=108u;
		 g_st_ram1.config_parameter.logic_control[0].u8_function_in3_add=109u;
		 g_st_ram1.config_parameter.logic_control[0].u8_function_in4_add=0u;
		 g_st_ram1.config_parameter.logic_control[0].u8_function_out_add=0u;
		 g_st_ram1.config_parameter.logic_control[0].u16_time_pra = 0u;
		 g_st_ram1.config_parameter.logic_control[0].u16_axis_x=455;
		 g_st_ram1.config_parameter.logic_control[0].u16_axis_y=651;
		
		 g_st_ram2.config_parameter.logic_control[0].u8_function_type=102u;   
		 g_st_ram2.config_parameter.logic_control[0].u8_function_in1_add=107u;
		 g_st_ram2.config_parameter.logic_control[0].u8_function_in2_add=108u;
		 g_st_ram2.config_parameter.logic_control[0].u8_function_in3_add=109u;
		 g_st_ram2.config_parameter.logic_control[0].u8_function_in4_add=0u;
		 g_st_ram2.config_parameter.logic_control[0].u8_function_out_add=0u;
		 g_st_ram2.config_parameter.logic_control[0].u16_time_pra = 0u;
		 g_st_ram2.config_parameter.logic_control[0].u16_axis_x=455;
		 g_st_ram2.config_parameter.logic_control[0].u16_axis_y=651;

		 /* *************************2:A&B&c ********************/
		 g_st_ram1.config_parameter.logic_control[1].u8_function_type=102u;   //A&B&c
		 g_st_ram1.config_parameter.logic_control[1].u8_function_in1_add=104u;
		 g_st_ram1.config_parameter.logic_control[1].u8_function_in2_add=105u;
		 g_st_ram1.config_parameter.logic_control[1].u8_function_in3_add=106u;
		 g_st_ram1.config_parameter.logic_control[1].u8_function_in4_add=0u;
		 g_st_ram1.config_parameter.logic_control[1].u8_function_out_add=0u;
		 g_st_ram1.config_parameter.logic_control[1].u16_time_pra = 0u;
		 g_st_ram1.config_parameter.logic_control[1].u16_axis_x=467;
		 g_st_ram1.config_parameter.logic_control[1].u16_axis_y=448;
		 
		 g_st_ram2.config_parameter.logic_control[1].u8_function_type=102u;   
		 g_st_ram2.config_parameter.logic_control[1].u8_function_in1_add=104u;
		 g_st_ram2.config_parameter.logic_control[1].u8_function_in2_add=105u;
		 g_st_ram2.config_parameter.logic_control[1].u8_function_in3_add=106u;
		 g_st_ram2.config_parameter.logic_control[1].u8_function_in4_add=0u;
		 g_st_ram2.config_parameter.logic_control[1].u8_function_out_add=0u;
		 g_st_ram2.config_parameter.logic_control[1].u16_time_pra = 0u;
		 g_st_ram2.config_parameter.logic_control[1].u16_axis_x=467;
		 g_st_ram2.config_parameter.logic_control[1].u16_axis_y=448;
		 /* 3:A&B&c */
		 g_st_ram1.config_parameter.logic_control[2].u8_function_type=102u;   //A&B&c
		 g_st_ram1.config_parameter.logic_control[2].u8_function_in1_add=101u;
		 g_st_ram1.config_parameter.logic_control[2].u8_function_in2_add=102u;
		 g_st_ram1.config_parameter.logic_control[2].u8_function_in3_add=103u;
		 g_st_ram1.config_parameter.logic_control[2].u8_function_in4_add=0u;
		 g_st_ram1.config_parameter.logic_control[2].u8_function_out_add=0u;
		 g_st_ram1.config_parameter.logic_control[2].u16_time_pra = 0u;
		 g_st_ram1.config_parameter.logic_control[2].u16_axis_x=469;
		 g_st_ram1.config_parameter.logic_control[2].u16_axis_y=220;
																							
		 g_st_ram2.config_parameter.logic_control[2].u8_function_type=102u;   
		 g_st_ram2.config_parameter.logic_control[2].u8_function_in1_add=101u;
		 g_st_ram2.config_parameter.logic_control[2].u8_function_in2_add=102u;
		 g_st_ram2.config_parameter.logic_control[2].u8_function_in3_add=103u;
		 g_st_ram2.config_parameter.logic_control[2].u8_function_in4_add=0u;
		 g_st_ram2.config_parameter.logic_control[2].u8_function_out_add=0u;
		 g_st_ram2.config_parameter.logic_control[2].u16_time_pra = 0u;
		 g_st_ram2.config_parameter.logic_control[2].u16_axis_x=469;
		 g_st_ram2.config_parameter.logic_control[2].u16_axis_y=220;
		  /* 4:A&B&c */
		 g_st_ram1.config_parameter.logic_control[3].u8_function_type=102u;   //A&B&c
		 g_st_ram1.config_parameter.logic_control[3].u8_function_in1_add=3u;
		 g_st_ram1.config_parameter.logic_control[3].u8_function_in2_add=2u;
		 g_st_ram1.config_parameter.logic_control[3].u8_function_in3_add=1u;
		 g_st_ram1.config_parameter.logic_control[3].u8_function_in4_add=0u;
		 g_st_ram1.config_parameter.logic_control[3].u8_function_out_add=0u;
		 g_st_ram1.config_parameter.logic_control[3].u16_time_pra = 0u;
		 g_st_ram1.config_parameter.logic_control[3].u16_axis_x=674;
		 g_st_ram1.config_parameter.logic_control[3].u16_axis_y=438;
																							
		 g_st_ram2.config_parameter.logic_control[3].u8_function_type=102u;   
		 g_st_ram2.config_parameter.logic_control[3].u8_function_in1_add=3u;
		 g_st_ram2.config_parameter.logic_control[3].u8_function_in2_add=2u;
		 g_st_ram2.config_parameter.logic_control[3].u8_function_in3_add=1u;
		 g_st_ram2.config_parameter.logic_control[3].u8_function_in4_add=0u;
		 g_st_ram2.config_parameter.logic_control[3].u8_function_out_add=0u;
		 g_st_ram2.config_parameter.logic_control[3].u16_time_pra = 0u;
		 g_st_ram2.config_parameter.logic_control[3].u16_axis_x=674;
		 g_st_ram2.config_parameter.logic_control[3].u16_axis_y=438;
		 
		 /* 5:A&B */
		 g_st_ram1.config_parameter.logic_control[4].u8_function_type=101u;   
		 g_st_ram1.config_parameter.logic_control[4].u8_function_in1_add=4u;
		 g_st_ram1.config_parameter.logic_control[4].u8_function_in2_add=110u;
		 g_st_ram1.config_parameter.logic_control[4].u8_function_in3_add=0u;
		 g_st_ram1.config_parameter.logic_control[4].u8_function_in4_add=0u;
		 g_st_ram1.config_parameter.logic_control[4].u8_function_out_add=60u;
		 g_st_ram1.config_parameter.logic_control[4].u16_time_pra = 0u;
		 g_st_ram1.config_parameter.logic_control[4].u16_axis_x=832;
		 g_st_ram1.config_parameter.logic_control[4].u16_axis_y=566;
																							
		 g_st_ram2.config_parameter.logic_control[4].u8_function_type=101u;     
		 g_st_ram2.config_parameter.logic_control[4].u8_function_in1_add=4u;
		 g_st_ram2.config_parameter.logic_control[4].u8_function_in2_add=110u;
		 g_st_ram2.config_parameter.logic_control[4].u8_function_in3_add=0u;
		 g_st_ram2.config_parameter.logic_control[4].u8_function_in4_add=0u;
		 g_st_ram2.config_parameter.logic_control[4].u8_function_out_add=60u;
		 g_st_ram2.config_parameter.logic_control[4].u16_time_pra = 0u;
		 g_st_ram2.config_parameter.logic_control[4].u16_axis_x=832;
		 g_st_ram2.config_parameter.logic_control[4].u16_axis_y=566;
		 
		 /* 输出缺省值 */ 
		 g_st_ram1.config_parameter.out_add[0].u16_output_x = 1024;
		 g_st_ram1.config_parameter.out_add[0].u16_output_y = 380;
		 g_st_ram2.config_parameter.out_add[0].u16_output_x = 1024;
		 g_st_ram2.config_parameter.out_add[0].u16_output_y = 380;
		 
		 g_st_ram1.config_parameter.out_add[1].u16_output_x = 1024;
		 g_st_ram1.config_parameter.out_add[1].u16_output_y = 500;
		 g_st_ram2.config_parameter.out_add[1].u16_output_x = 1024;
		 g_st_ram2.config_parameter.out_add[1].u16_output_y = 500;
		 
		 g_st_ram1.config_parameter.out_add[2].u16_output_x = 1024;
		 g_st_ram1.config_parameter.out_add[2].u16_output_y = 620;
		 g_st_ram2.config_parameter.out_add[2].u16_output_x = 1024;
		 g_st_ram2.config_parameter.out_add[2].u16_output_y = 620;
		 
		 g_st_ram1.config_parameter.out_add[3].u16_output_x = 1024;
		 g_st_ram1.config_parameter.out_add[3].u16_output_y = 740;
		 g_st_ram2.config_parameter.out_add[3].u16_output_x = 1024;
		 g_st_ram2.config_parameter.out_add[3].u16_output_y = 740;
		 
		 /* 校验和版本号 */	 		 
     g_st_ram1.config_parameter.u16_config_crc      =  0x633Au;    //0x2CA1u    0x7B0Eu
	   g_st_ram2.config_parameter.u16_config_crc      =  0x633Au;  
		 g_st_ram1.config_parameter.u8_software_ver =88u;   //固件版本  20230727
		 g_st_ram2.config_parameter.u8_software_ver =88u;   //固件版本  20230727
	}
	else
	{
		/* Program is right! */
		prg_copy_ram_BtoA((uint8_t *)&g_st_ram1,(uint8_t *)&g_u16_flash_dat,u16_tmp1+u16_tmp3);
		prg_copy_ram_BtoA((uint8_t *)&g_st_ram2,(uint8_t *)&g_u16_flash_dat,u16_tmp1+u16_tmp3);
	}
}





void prg_copy_ram_BtoA(uint8_t *ram_a, uint8_t *ram_b, uint16_t u16_len)
{
	/*config_flsah是FLSAH中组态数据，实际已放到RAM中，本质上还是RAM TO RAM*/
	uint16_t	u16_idx		= 0u;

	uint8_t	*u8p_tmp1	= (uint8_t *)ram_a;
	uint8_t	*u8p_tmp2	= (uint8_t *)ram_b;

	for(u16_idx = 0u; u16_idx < u16_len; u16_idx ++)
	{
		*u8p_tmp1=*u8p_tmp2;
		u8p_tmp1++;
		u8p_tmp2++;
		
	}
}

///* 0x4B37 */
//uint16_t calc_crc16(const uint8_t u8a_dat[], uint16_t u16_len)
//{
//	const uint8_t c_u8a_auc_crc_hi[] = {
//		0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x01u, 0xC0u, 0x80u, 0x41u,
//		0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x00u, 0xC1u, 0x81u, 0x40u,
//		0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x01u, 0xC0u, 0x80u, 0x41u,
//		0x00u, 0xC1u, 0x81u, 0x40u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u,
//		0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x01u, 0xC0u, 0x80u, 0x41u,
//		0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x00u, 0xC1u, 0x81u, 0x40u,
//		0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x00u, 0xC1u, 0x81u, 0x40u,
//		0x01u, 0xC0u, 0x80u, 0x41u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x00u, 0xC1u, 0x81u, 0x40u,
//		0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x01u, 0xC0u, 0x80u, 0x41u,
//		0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x00u, 0xC1u, 0x81u, 0x40u,
//		0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x01u, 0xC0u, 0x80u, 0x41u,
//		0x00u, 0xC1u, 0x81u, 0x40u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 
//		0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x01u, 0xC0u, 0x80u, 0x41u,
//		0x00u, 0xC1u, 0x81u, 0x40u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 
//		0x01u, 0xC0u, 0x80u, 0x41u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u,
//		0x00u, 0xC1u, 0x81u, 0x40u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u,
//		0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x01u, 0xC0u, 0x80u, 0x41u, 
//		0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x00u, 0xC1u, 0x81u, 0x40u,
//		0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x01u, 0xC0u, 0x80u, 0x41u,
//		0x00u, 0xC1u, 0x81u, 0x40u, 0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u,
//		0x00u, 0xC1u, 0x81u, 0x40u, 0x01u, 0xC0u, 0x80u, 0x41u, 0x01u, 0xC0u, 0x80u, 0x41u,
//		0x00u, 0xC1u, 0x81u, 0x40u
//	};

//	const uint8_t c_u8a_auc_crc_lo[] = {
//		0x00u, 0xC0u, 0xC1u, 0x01u, 0xC3u, 0x03u, 0x02u, 0xC2u, 0xC6u, 0x06u, 0x07u, 0xC7u,
//		0x05u, 0xC5u, 0xC4u, 0x04u, 0xCCu, 0x0Cu, 0x0Du, 0xCDu, 0x0Fu, 0xCFu, 0xCEu, 0x0Eu,
//		0x0Au, 0xCAu, 0xCBu, 0x0Bu, 0xC9u, 0x09u, 0x08u, 0xC8u, 0xD8u, 0x18u, 0x19u, 0xD9u,
//		0x1Bu, 0xDBu, 0xDAu, 0x1Au, 0x1Eu, 0xDEu, 0xDFu, 0x1Fu, 0xDDu, 0x1Du, 0x1Cu, 0xDCu,
//		0x14u, 0xD4u, 0xD5u, 0x15u, 0xD7u, 0x17u, 0x16u, 0xD6u, 0xD2u, 0x12u, 0x13u, 0xD3u,
//		0x11u, 0xD1u, 0xD0u, 0x10u, 0xF0u, 0x30u, 0x31u, 0xF1u, 0x33u, 0xF3u, 0xF2u, 0x32u,
//		0x36u, 0xF6u, 0xF7u, 0x37u, 0xF5u, 0x35u, 0x34u, 0xF4u, 0x3Cu, 0xFCu, 0xFDu, 0x3Du,
//		0xFFu, 0x3Fu, 0x3Eu, 0xFEu, 0xFAu, 0x3Au, 0x3Bu, 0xFBu, 0x39u, 0xF9u, 0xF8u, 0x38u, 
//		0x28u, 0xE8u, 0xE9u, 0x29u, 0xEBu, 0x2Bu, 0x2Au, 0xEAu, 0xEEu, 0x2Eu, 0x2Fu, 0xEFu,
//		0x2Du, 0xEDu, 0xECu, 0x2Cu, 0xE4u, 0x24u, 0x25u, 0xE5u, 0x27u, 0xE7u, 0xE6u, 0x26u,
//		0x22u, 0xE2u, 0xE3u, 0x23u, 0xE1u, 0x21u, 0x20u, 0xE0u, 0xA0u, 0x60u, 0x61u, 0xA1u,
//		0x63u, 0xA3u, 0xA2u, 0x62u, 0x66u, 0xA6u, 0xA7u, 0x67u, 0xA5u, 0x65u, 0x64u, 0xA4u,
//		0x6Cu, 0xACu, 0xADu, 0x6Du, 0xAFu, 0x6Fu, 0x6Eu, 0xAEu, 0xAAu, 0x6Au, 0x6Bu, 0xABu, 
//		0x69u, 0xA9u, 0xA8u, 0x68u, 0x78u, 0xB8u, 0xB9u, 0x79u, 0xBBu, 0x7Bu, 0x7Au, 0xBAu,
//		0xBEu, 0x7Eu, 0x7Fu, 0xBFu, 0x7Du, 0xBDu, 0xBCu, 0x7Cu, 0xB4u, 0x74u, 0x75u, 0xB5u,
//		0x77u, 0xB7u, 0xB6u, 0x76u, 0x72u, 0xB2u, 0xB3u, 0x73u, 0xB1u, 0x71u, 0x70u, 0xB0u,
//		0x50u, 0x90u, 0x91u, 0x51u, 0x93u, 0x53u, 0x52u, 0x92u, 0x96u, 0x56u, 0x57u, 0x97u,
//		0x55u, 0x95u, 0x94u, 0x54u, 0x9Cu, 0x5Cu, 0x5Du, 0x9Du, 0x5Fu, 0x9Fu, 0x9Eu, 0x5Eu,
//		0x5Au, 0x9Au, 0x9Bu, 0x5Bu, 0x99u, 0x59u, 0x58u, 0x98u, 0x88u, 0x48u, 0x49u, 0x89u,
//		0x4Bu, 0x8Bu, 0x8Au, 0x4Au, 0x4Eu, 0x8Eu, 0x8Fu, 0x4Fu, 0x8Du, 0x4Du, 0x4Cu, 0x8Cu,
//		0x44u, 0x84u, 0x85u, 0x45u, 0x87u, 0x47u, 0x46u, 0x86u, 0x82u, 0x42u, 0x43u, 0x83u,
//		0x41u, 0x81u, 0x80u, 0x40u
//	};
//	
//	uint8_t		u8_crc_hi = 0xFFu;
//	uint8_t		u8_crc_lo = 0xFFu;
//	uint16_t	u16_crc_val	= 0u;
//	uint16_t	u16_data_idx	= 0u;
//	uint16_t	u16_table_idx	= 0u;

//	u16_data_idx	= 0u;
//	u16_table_idx	= 0u;
//    while(u16_data_idx < u16_len)
//    {
//    	u8_crc_lo = u8_crc_lo ^ u8a_dat[u16_data_idx];
//        u16_table_idx = (uint16_t)(u8_crc_lo);
//        u8_crc_lo = ( uint8_t )( u8_crc_hi ^ c_u8a_auc_crc_hi[u16_table_idx] );
//        u8_crc_hi = c_u8a_auc_crc_lo[u16_table_idx];
//		u16_data_idx ++;
//    }
//	u16_crc_val	= (uint16_t)((uint16_t)u8_crc_hi << 8u);
//    u16_crc_val	= (uint16_t)(u16_crc_val | (uint16_t)u8_crc_lo );
//	
//	return u16_crc_val;
//}

///* 0x4B37 */
//uint16_t prg_calc_crc16(const mb_app_iec_dat_t *stp_prg)
//{
//	uint16_t	u16_crc_val	= 0u;
//	uint16_t	u16_len		= 0u;
//	
//	u16_len	= sizeof(prg_dat_t);
//	u16_len	= (uint16_t)(u16_len << 1u);
//	u16_len	= u16_len + sizeof(inst_parm_t);
//	
//	/* u16_crc_val = calc_crc16((const uint8_t*)stp_prg, u16_len); */
//	u16_crc_val = usMBCRC16((uint8_t*)stp_prg, u16_len);

//	return u16_crc_val;
//}

